Jump to content
Sign in to follow this  

Java Applets, creating an image outside of paint()

Recommended Posts

I'm relatively new to Java programming, to a certain extent. More specifically, new to programming applets. I used to do Java programming somewhat about three years ago, but not too regularly.Anyway, I had attempted to create an image and display it to the screen outside of the paint() function. It is in a manually created method called "update_text".

m_letter1= getImage(getCodeBase(), ut_character);m_graphics1.drawImage(m_letter1, 21, 21, this);

This executes at the end of the update_text method. However, I get a NullExceptionError when it runs, and therefore the image is not displayed. Here (before the above code is executed), apparently m_letter1 and m_graphics1 are null. ut_character is not. After executing the above code, only m_graphics1 is null.Outside of the update_text method, I defined the graphics and image variables (m_letter1 and m_graphics1):

private Image m_letter1;private Graphics m_graphics1;

This is the error I get:

Exception in thread "Thread-5" java.lang.NullPointerExceptionat base.Main.update_text(Main.java:113)at base.Main.run(Main.java:156)at java.lang.Thread.run(Thread.java:619)

Main.java:113: m_graphics1.drawImage(m_letter1, 21, 21, this);Main.java.156: update_text();Anyway, I was wondering if there was a way to fix this. I can't necessarily use the paint method, I'm pretty sure, since I don't want all of this displayed exactly at run time.Any help would be much appreciated.

Share this post

Link to post
Share on other sites

NullPointerExceptions usually occur when you try to reference something that is null. That image or Graphics class may need to be created in memory by using the "new" keyword.Like:

m_letter1 = new Image();

.Then you would use the getImage method. I believe those two classes are created like that so check the documentation for constructors. Image Class

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...