-1

****It throws NullPointerException in JOptionPane code as following.****

public static void main(String[]args){


    String[] options={"Do it again?", "Exit"};
    int response=0;
    int indexOfZip;
    do{
        try{

        String dataStr=JOptionPane.showInputDialog("Enter a zip code");


        if(dataStr!=null){
             while (k<i){
                 if (place[k].equals(dataStr)){

                 indexOfZip=k;
                     k++;
                 }

             }
             int data=Integer.parseInt(dataStr);
             response=JOptionPane.showOptionDialog(null,
                           "You asked me to search for zip code:"+dataStr+"The zip code "+dataStr+" belongs to"+place[k].getTown()+","+place[k].getState()+"Do you want me to search again?",
                           "results",

                           JOptionPane.YES_NO_OPTION,
                           JOptionPane.QUESTION_MESSAGE,
                           null,
                           options,options[0]);


             System.out.println("You asked me to search for zip code:"+dataStr);
             System.out.println("The zip code "+dataStr+" belongs to"+place[k].getTown()+","+place[k].getState());
             System.out.println("Do you want me to search again?");

        }


        }catch(NumberFormatException e){
        JOptionPane.showMessageDialog(null,"Bad Numeric String, try again.", "Input Error",JOptionPane.ERROR_MESSAGE);

         }
    }while(response==0);
    if (response!=0){
       System.out.print("No");
       System.out.println("Good Bye!");
    }



    }

The terminal says the exception is for this line response=JOptionPane.showOptionDialog(null,

But I cannot see anything wrong here, because the "response" is initialized before.

ksdawq
  • 29
  • 1
  • 5
  • What's `place=[k]`? Is it null? If not, are its fields null? Post your full stacktrace and double check that you're looking at the correct involved line. Do null tests **before** the involved line to see what's null, and then figure out why. – Hovercraft Full Of Eels Sep 26 '15 at 17:49
  • @PavelUvarov Sorry i m new to code. Where am i supposed to declare indexOfZip? – ksdawq Sep 26 '15 at 18:01
  • It depends on fact - where you use it. Now it has no usages, so this variable can be simply omitted. But according to other sources part I see some variables that weren't declared, but used. For example - `i`. Common way to decide where you should place variable - is decide on 2 characteristics of visibility - it should be not less than sufficient, but not more than required. Additionally code styles defines that for methods is not recommended to be bigger than 10-12 lines. And it is not recommended to have more than 2 levels of nesting blocks in methods. I recommend you read some code styles. – Pavel Uvarov Sep 26 '15 at 18:15

1 Answers1

0

It looks like place[k] must be null, and place[k].getTown() triggers NullPointerException. Watch the value of place[k] in a debugger (or add a print statement).

janos
  • 120,954
  • 29
  • 226
  • 236
  • 1
    Do you know what ?? `place[k]` is a String. How come String have those methods ? – Suresh Atta Sep 26 '15 at 17:50
  • We don't see the declaration of `place`, so we don't know what it is. If it's a string than we're looking at non-compiling code and all bets are off. – janos Sep 26 '15 at 17:52
  • I see. I forgot to call the method of place[k]. Now I change the code to if (place[k].getZip().equals(dataStr)){ indexOfZip=k; k++; } but it still has the same exception ... – ksdawq Sep 26 '15 at 17:57
  • Looks like `place[k]` is `null`. You can't call anything on it while it's `null`. – janos Sep 26 '15 at 17:59