Results 1 to 8 of 8
Thread: Weird NullPointerException
- 06-22-2011, 04:05 AM #1
[SOLVED] Weird NullPointerException
EDIT: It's not weird, I am weird, DarrylBurke says. I still need help though :P
EDIT EDIT: Solved - global variables get initialized before the class itself is initialized. Moving the initialization to a method solved the problem.
Hey, I am currently working on a chat bot with GUI. However, when I try to display the lines received in a pane, I get a NullPointerException
public void read(){
System.out.println() works fine, but the line after it (gui.appendRaw()) causes the exception.Java Code:try { reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); while (true) { String line; if ((line = reader.readLine()) != null) { System.out.println(line); gui.appendRaw(line); } } } catch (IOException e) { gui.logError(e); } }
Whole class can be found here (Bot.java) and here (Line 325+) (GUI.java)Last edited by Boreeas; 07-12-2011 at 11:29 AM. Reason: Solved
- 06-22-2011, 04:28 AM #2
Then gui must be null. you will have to investigate why. We cannot tell from the small snippet you posted.
- 06-22-2011, 04:43 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Many people won't follow links, or wade through large amounts of code, you can post the full code if you'd like, but don't expect people to read it(some may). It would be helpful if you created a smaller version that illustrates your example.
Also, using print statements can be invaluable, what happens if you print the GUI object?
- 06-22-2011, 07:33 AM #4
And there's nothing wierd about a NullPointerException, but I sometimes feel the people who regard exceptions as wierd are, well, you guessed it.
db
- 06-22-2011, 04:03 PM #5
Hmm, good point, thanks.
Printing the gui gives me - you guessed it - null.
I declare it using
and Instances.getGuiInstance() returns a static gui object (new GUI();) Is the "static" part the problem? I'm not good with static/nonstatic stuff :/Java Code:private GUI gui = Instances.getGuiInstance();
Java Code:public static GUI gui = new GUI(); public static GUI getGuiInstance() { return gui; }
- 06-22-2011, 05:16 PM #6
Try adding a print to show if it is valid or changed
Java Code:public static GUI getGuiInstance() { System.out.println("gui=" + gui); // show current contents of gui return gui;
- 07-06-2011, 07:55 AM #7
Alright, I did that:
GUI = none when called from BaseBot.java (Note:As a global variable) and apparently correct the other two times when being called from GUI.java (From functions, that time)Java Code:GUI = null GUI = ircbotgui.GUI[frame0,0,0,1511x826,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=javax.swing.JRootPane[,8,30,1495x788,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true] GUI = ircbotgui.GUI[frame0,0,0,1511x826,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=javax.swing.JRootPane[,8,30,1495x788,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
- 07-12-2011, 11:30 AM #8
Similar Threads
-
Weird crashes...
By illnin0 in forum New To JavaReplies: 3Last Post: 04-16-2011, 03:04 AM -
Weird UPS/FPS Issue
By AndrewM16921 in forum New To JavaReplies: 0Last Post: 04-05-2011, 11:14 PM -
Weird output
By gandalf5166 in forum New To JavaReplies: 2Last Post: 02-28-2010, 09:17 PM -
weird problem
By GPB in forum New To JavaReplies: 2Last Post: 02-28-2010, 12:04 PM -
Seriously weird output
By gandalf5166 in forum Java AppletsReplies: 4Last Post: 02-27-2010, 04:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks