Results 1 to 5 of 5
  1. #1
    Jacinth is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default Null Pointer Exception

    Hello, I am newer to Java and new to these forums. I need help understanding why I keep getting a NullPointException.

    I've pulled the problem code into a small sample program:

    import javax.swing.JOptionPane;

    public class tester
    {
    public static void main(String args[])
    {
    boolean isDone = false;
    do
    {
    String input = JOptionPane.showInputDialog(null, "Please enter something");

    //If/Else statement so that when the user enters a 'Q' (non-case sensitive), the loop ends
    if (input.equalsIgnoreCase("Q"))
    isDone = true;
    else
    {
    if(!input.isEmpty())
    {
    //Gets the length of the user input
    int fieldLength = input.length();
    //Only tries to process a 4 character entry
    if(fieldLength == 4)
    {
    //Puts the entry into a Character array so we can test for digits
    Character[] inputChar = new Character[4];
    //Checks to see if the input was actually a number
    boolean areDigits = Character.isDigit(inputChar[0]) && Character.isDigit(inputChar[1]) && Character.isDigit(inputChar[2]) && Character.isDigit(inputChar[3]);
    if(areDigits)
    System.out.println("Entered 4 digits!");
    }
    }
    }

    }
    while(!isDone);
    }
    }
    I keep getting a NullPointerException at the 'boolean areDigits =...' line. I don't quite understand why this is happening. Can anyone explain what I have done wrong?

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Well, your comment says "Puts the entry into a Character array". But where's the line of code that actually does that? At the moment, you're just creating an array that initially will be filled with... nulls.

    By the way, be careful of the distinction between "char" (normally the data type used to represent a character), and "Character", used in very specific cases when you want to wrap a char inside an object.

  3. #3
    Jacinth is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    That makes a lot of sense...I can't believe I did that. I feel like a dummy now!

    I changed the array from a Character array to a char array, and used toCharArray() to put the input into the array. It seems to have worked!

    Thanks so much!

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I suggest using the methods provided by String, rather than converting to char[]. Converting makes you code more complex and less flexible.

    You could do
    Java Code:
    if (input.matches("\\d{4}")) {
      // do something
    }
    The matches expression is a "regular expression". The reason for the two backslashes is that String literals use backslash as an escape character.

    The \d checks for a digit. The {4} checks for exactly 4 of them. You could change the {4} to +, which says one or more digits.

    Regular expressions are very powerful, and they make your code clear and simple.
    Last edited by Steve11235; 01-22-2009 at 01:01 AM. Reason: Forgot to explain the regex

  5. #5
    Jacinth is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    I tried that it and it worked as well! It also eliminated a bunch of other code and - just like you said - makes it much more simple.

    Thank you very much for all of your help!

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 07:33 PM
  3. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM
  4. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 AM
  5. statement null pointer exception
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 05:23 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
  •