-3

Why is it when i input 1 or 2 to activate the switch case 1 or 2 i have a null pointer exception error?? i really need people who can shed some light to my problem. I believe there is nothing wrong with my switch cases. what did i miss??

   package dakilangtaoako;
   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStreamReader;
    import java.util.logging.Level;
   import java.util.logging.Logger;


 public class DakilangTaoAko {
public static void main(String[] args){

    BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
    String mydata[][] = new String[5][3] ;
    String inttoword [] = new String [5];
    int wordtoint[] = new int [5]; 
    int mychoice = 0;
    String toreturn = "";

    for (int outside = 0; outside < 5; outside++){
        System.out.println("Entry no." + (outside + 1));
        for (int inside = 0; inside < 3; inside++){
        if(inside == 0){
            try {
                System.out.print("Name: ");
                mydata[outside][inside]= dataIn.readLine();
            } catch (IOException ex) {
                Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.parse("OK"), null, ex);
            }
 }
        else if(inside == 1){
            try {
                System.out.print("ID: ");
                mydata[outside][inside]= dataIn.readLine();
            } catch (IOException ex) {
                Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.parse("OK"), null, ex);
            }
  }
        else if(inside ==2){
            try {
                System.out.print("Subject: ");
                mydata[outside][inside]= dataIn.readLine();
            } catch (IOException ex) {
                Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.parse("OK"), null, ex);
            }


 }
}
    }
 for(int outside= 0; outside <5; outside++){
            for(int inside = 1; inside <= 3; inside++){
                if(inside ==0)
                    inttoword[outside]=(mydata[outside][inside])+";"+(mydata[outside][inside]);
            }
       }
 for(int outside = 0; outside < 5; outside++){
            for(int inside = 1; inside <= 3; inside++){
                if(inside ==0)
                    wordtoint[outside]=Integer.parseInt(mydata[outside][inside]);
            }
       }


 for(int i=0;i<5;i++){
 wordtoint[i]=Integer.parseInt(mydata[i][1]);
 }

       while(mychoice != 5){
       System.out.println("-------- DISPLAY OPTIONS --------");
       System.out.println("\t [1] Display Entries Alphabetically ");
       System.out.println("\t [2] Display Entries by Ascending ID number ");
       System.out.println("\t [3] Display Students Enrolled in Prog2 ");
       System.out.println("\t [4] Display Students Enrolled in CSO ");
       System.out.println("\t [5] Exit ");
       System.out.println();
       System.out.print("\t Choice:");
            try {
                mychoice = Integer.parseInt(dataIn.readLine());
            } catch (IOException ex) {
                Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
            }

            switch (mychoice){



                case 1:
                  System.out.println("----Entries In Alphabetical Order----");
                  alphabetSorter(inttoword);
                        for(int limiter = 0; limiter < 5; limiter++)
               System.out.println(inttoword[limiter]);


                  System.out.print("Press [r] to Return to Main Menu...");
        try {
           toreturn = dataIn.readLine();
       } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
       }
                     if("r".equals(toreturn)) 
                    break;
                case 2:

                  System.out.println(" ---- Entries by Ascending ID Number ----");  
                        for(int outside = 0; outside < 5; outside++){
                        numSorter(wordtoint);
                        for(int inside = 0; inside < 5; inside++){
                        if(Integer.parseInt(mydata[inside][1])== wordtoint[outside])
                  System.out.println(wordtoint[outside] + ";" + mydata[inside][0]);
               }                
           } 
                   System.out.print("Press [r] to return to main Menu...");
        try {
           toreturn = dataIn.readLine();
       } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
       }            
                 if("r".equals(toreturn))
                         break;

                case 3:
                    System.out.println(" ---- Students Enrolled in Prog2 ----");
                    String mustfind1 = "PROG2";     
                    for(int outside = 0; outside < 5; outside++){
                    if(mydata[outside][2].equalsIgnoreCase(mustfind1)){
                    System.out.println(mydata[outside][0]+";"+mydata[outside][1]);
                                        }
                        }System.out.print("Press [r] to Return to Main Menu...");
        try {
           toreturn = dataIn.readLine();
       } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
       }
                  if("r".equals(toreturn))   
                    break;

                case 4:
                    System.out.println(" ---- Students Enrolled in CSO ---- ");
                    String mustfind2 = "CSO";
                    for(int outside = 0; outside < 5; outside++){
                        if(mydata[outside][2].equalsIgnoreCase(mustfind2))
                            System.out.println(mydata[outside][0]+";"+mydata[outside][1]);
                                        }
                        System.out.print("Press [r] to Return to Main Menu...");
       try {
           toreturn = dataIn.readLine();
       } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
       }
                  if("r".equals(toreturn))
                    break;

                case 5:
                    mychoice = 5;

                    break;  

                default:
                    System.out.println("Sorry the Number is Not in the Choices!!");
                    System.out.print("Press [r] to Return to Main Menu...");
        try {
           toreturn = dataIn.readLine();
             } catch (IOException ex) {
           Logger.getLogger(DakilangTaoAko.class.getName()).log(Level.SEVERE, null, ex);
          }
                 if("r".equals(toreturn))
                     break;
            }     }   
   }
       public static void numSorter(int given[]){

  int temp;
  for(int outside = 0; outside < given.length-1; outside++)
    {
        for(int inside = outside+1 ; inside < given.length; inside++)
        {  
            if(given[outside] > given[inside])
            {                                         
            temp = given[outside];
            given[outside]= given[inside];  
            given[inside]=temp;                                    
            } 
        } 
    } 
    }
    public static void alphabetSorter(String given[]){

String temp;
for(int outside = 0; outside < given.length -1; outside++)
    {
        for(int inside = outside +1; inside < given.length; inside++)
        {  
            if(given[outside].compareToIgnoreCase(given[inside])>0)
            {                                         
            temp= given[outside];
            given[outside]= given[inside];  
            given[inside]=temp;                                    
            } 
        } 
      } 
 }




  }
Andy G
  • 18,518
  • 5
  • 42
  • 63
  • Look into log file because you are using Logger or simply use `ex.printStackTrace()` while development. – Braj May 16 '14 at 21:56
  • That's a lot of code; please try narrowing the problem down. – arshajii May 16 '14 at 21:56
  • You might be interested in [this lightweight java debugging tutorial](http://keysersblog.wordpress.com/2014/04/21/debugging-java-code-a-beginners-guide/) I wrote. People tend not to be very helpful if you haven't narrowed down your problem. – keyser May 16 '14 at 21:56
  • 5
    "Nearly accurate" and "accurate" mean opposite things. – nhgrif May 16 '14 at 21:57
  • Exception in thread "main" java.lang.NullPointerException at dakilangtaoako.DakilangTaoAko.alphabetSorter(DakilangTaoAko.java:195) at dakilangtaoako.DakilangTaoAko.main(DakilangTaoAko.java:91) Java Result: 1 – user3645333 May 16 '14 at 21:58
  • 'public static void alphabetSorter(String given[]){ String temp; for(int outside = 0; outside < given.length -1; outside++) { for(int inside = outside +1; inside < given.length; inside++) { if(given[outside].compareToIgnoreCase(given[inside])>0) { temp= given[outside]; given[outside]= given[inside]; given[inside]=temp; } } } }' As i understand , the problem is specifically here i just cant pinpoint it – user3645333 May 16 '14 at 22:01
  • @user3645333 That's way more than one line. – awksp May 16 '14 at 22:07
  • if(given[outside].compareToIgnoreCase(given[inside])>0) this is my problem – user3645333 May 16 '14 at 22:16

1 Answers1

0

It is because you are not initializing the array inttoword. All its entries are null and that will choke this statement:

if (given[outside].compareToIgnoreCase(given[inside]) > 0)

you are asking the jdk to do: if (null.compareToIgnoreCase(null) > 0)

Alexandre Santos
  • 7,654
  • 7
  • 35
  • 60