Results 1 to 8 of 8
  1. #1
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default [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(){

    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);
            } 
        }
    System.out.println() works fine, but the line after it (gui.appendRaw()) causes the exception.
    Whole class can be found here (Bot.java) and here (Line 325+) (GUI.java)
    Last edited by Boreeas; 07-12-2011 at 12:29 PM. Reason: Solved

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Then gui must be null. you will have to investigate why. We cannot tell from the small snippet you posted.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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

  5. #5
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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?
    Hmm, good point, thanks.
    Printing the gui gives me - you guessed it - null.
    I declare it using
    Java Code:
    private GUI gui = Instances.getGuiInstance();
    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:
    public static GUI gui = new GUI();
    
    public static GUI getGuiInstance() {
    
        return gui;
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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;

  7. #7
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Alright, I did that:
    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]
    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)

  8. #8
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Solved - global variables get initialized before the class itself is initialized. Moving the initialization to a method solved the problem.

Similar Threads

  1. Weird crashes...
    By illnin0 in forum New To Java
    Replies: 3
    Last Post: 04-16-2011, 04:04 AM
  2. Weird UPS/FPS Issue
    By AndrewM16921 in forum New To Java
    Replies: 0
    Last Post: 04-06-2011, 12:14 AM
  3. Weird output
    By gandalf5166 in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 10:17 PM
  4. weird problem
    By GPB in forum New To Java
    Replies: 2
    Last Post: 02-28-2010, 01:04 PM
  5. Seriously weird output
    By gandalf5166 in forum Java Applets
    Replies: 4
    Last Post: 02-27-2010, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •