Jump to content

Java Applets, creating an image outside of paint()


Darkness
 Share

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.

Link to comment
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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...