Results 1 to 6 of 6
  1. #1
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default NullPointerException in my while loop

    So I'm fooling around with objects and it's all going okay, however I have a small problem I'm facing right now. I want the user to input values in a dialog box over and over, until he presses "cancel", and then the program will print out the last entry. I get this to work if I loop a number of specific times ny incrementing a variable, but I can't get it to work until "cancel" is pressed.

    This is my main method:

    Java Code:
    package whatever;
    import javax.swing.JOptionPane;
    public class beta {
    public static void main(String[] args) {
    
        String out;
        int i = 0;
        String name = " ";
    
        while(name!=null){
    
            namn = JOptionPane.showInputDialog("Enter name and SSN ");
            PersonBeta[]anArray = new PersonBeta[2];
            anArray[0]= new PersonBeta(name);
            anArray[1]= new PersonBeta (name);
            PersonBeta.addPerson(1);
    
    
        }
    
    
        out = (PersonBeta.getInfo());
        JOptionPane.showMessageDialog(null, out);
    
    }
    
    }
    and this is my object

    Java Code:
    package whatever;
    
    public class PersonBeta {
    
    static String name;
    static String ssn;
    
    static int totalPersons = 0;
    
    PersonBeta(String n){
    
        String[]x = n.split(",");
        name = x[0];
        ssn = x[1];
    
    }
    public static String getInfo(){
    
        return "You've given " + totalPersons + " persons. The latest person you gave was " + name + ", with social security number " + ssn + "."  ;                            
    
    }
    public static void addPerson(int amount){
    
        totalPersons = totalPersons + amount;
    }
    
    }
    What is the best way to go about this? Any help would be highly appreciated!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: NullPointerException in my while loop

    NullPointerException in my while loop
    What variable has the null value? Can the code test for a null value before using the variable?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: NullPointerException in my while loop

    The variable "name". It says that the errors are on two places: 1) in my main method at line 14 and 2) in my object class at line 12.

    And no, if I press cancel immediately without using the variable, it throws the same error message.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: NullPointerException in my while loop

    Quote Originally Posted by iwtljava View Post
    And no, if I press cancel immediately without using the variable, it throws the same error message.
    That's because your inputDialog returns null but you still try to use it. You should test for null and use break to get out of the loop.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: NullPointerException in my while loop

    I already tried adding this in the while loop, and changing the while condition to (true)

    Java Code:
    if(name==null) break;
    with no success, it still throws the same error

  6. #6
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: NullPointerException in my while loop

    Oh my god I'm stupid! I just had the if-block at the wrong place...... now it's working like it should... feel like an idiot

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 10:27 AM
  2. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  4. NullPointerException in a double loop
    By Bloitz in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 02:11 PM
  5. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM

Posting Permissions

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