Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy java.lang.NullPointerException

    Hello guys i've got a little problem

    Exception in thread "main" java.lang.NullPointerException
    at TestMyUtils.InsSort(TestMyUtils.java:251)
    at TestMyUtils.main(TestMyUtils.java:330)
    Java Result: 1
    line 251
    while(position>0 && myBook[position-1].getOnoma().compareToIgnoreCase(key.getOnoma())>0){

    line 330
    case 2: InsSort(myBook, 'a');

    whole code
    import java.io.*;
    class TestMyUtils{ 
       public static int metritis = 0;
       public static int metritis2 = 0;
       public static long y; 
     
       public static void EisagVivliou(Book[] myBook){      
          if(metritis<10){
            myBook[metritis]=new Book();
            System.out.println("Dose Titlo: ");
            myBook[metritis].setTitlo(UserInput.getString());
            System.out.println("Dose Suggrafea: ");
            myBook[metritis].setOnoma(UserInput.getString());
            System.out.println("Dose ISBN: ");
            System.out.println();
            boolean flag2=false;
            do{ 
              do{   
              System.out.println("***Sosto 10psifio h' 13psifio");
              myBook[metritis].setIsbn(UserInput.getString());
              y= Long.parseLong(myBook[metritis].getIsbn());
              } while( (y<999999999L || y>10000000000L) && (y<999999999999L || y>100000000000000L) ); 
              if(y<100000000000L){
              flag2 = elegxos10(y, flag2);
              } else{
              flag2 = elegxos13(y, flag2);
              }
            } while( flag2 == false);
            System.out.println("Dose Ekdotiko Oiko: ");
            myBook[metritis].setOikos(UserInput.getString());
            System.out.println("Dose Arithmo Selifon: ");
            myBook[metritis].setSelides(UserInput.getInteger());
            System.out.println("Dose Etsos Ekdosis");
            myBook[metritis].setEtos(UserInput.getInteger());
            System.out.println("Dose Timi: ");
            myBook[metritis].setTimi(UserInput.getDouble());
            ++metritis;
          }
          else System.out.println("Gemisame");
        }
       public static void ListaEpilogon(){
          System.out.println();
          System.out.println("Lista Epilogon");
          System.out.println("1. Eisagogi Stoixeion Vivliou");
          System.out.println("2. Anazitisi Vivliou");
          System.out.println("3. Emfanisi Stoixeion Vivliou me timi meta3u kapoion orion");
          System.out.println("4. Taksinomisi Biblion");
          System.out.println("5. Emfanisi stoixeion olon ton vivlion");
          System.out.println("6. Telos");
          System.out.println("Doste Epilogi (1-5): ");
      }
       public static void EpilogiPedio(){
          System.out.println();  
          System.out.println("Epilogi Pedio Anazitisis");
          System.out.println("1. ISBN");
          System.out.println("2. Etos Kukloforias");
          System.out.println("3. Epistrofi stin arxiki lista epilogon");
          System.out.println("Doste epilogi (1-3): ");
        }
       public static void EpilogiMeth(){
          System.out.println();  
          System.out.println("Epilogi Methedou Anazitisi");
          System.out.println("1. Seiriaki Anazitisi");
          System.out.println("2. Duadiki Anazitisi");
          System.out.println("3. Epistrofi Stin Epilogi Pediou Anazitisis");
          System.out.println("Doste Epilogi (1-3)");
        }
       public static void EpilogiTaks1(){
          System.out.println();  
          System.out.println("Epilogi Pediou Taksinomisis");
          System.out.println("1. Onoma Suggrafea");
          System.out.println("2. ISBN");
          System.out.println("3. Etsos Kukloforias");
          System.out.println("4. Timi Vivliou");
          System.out.println("5. Epistrofi Stin Arxiki Lista Epilogon");
          System.out.println("Doste Epilogi (1-5)");
        }
       public static void EpilogiTaks2(){
          System.out.println();  
          System.out.println("Epilogi Methodou");
          System.out.println("1. Bubblesort");
          System.out.println("2. Taksinomisi me eisagogi");
          System.out.println("3. Taksinomisi me epilogi");
          System.out.println("4. Grigori taksinomisi");
          System.out.println("5. Taksinomisi me sugxoneusi");
          System.out.println("6. Epistrofi stin epilogi pediou taksinomisi");
          System.out.println("Doste Epilogi (1-6)");
        }
       public static long seirAnazit(Book[] myBook, String key){       
            for (int i=0; i<=metritis; i++) { 
                if (myBook[i].getIsbn().equals(key)) {
                    return i;
                } else return -1;
            }       
            return -1;
        }
       public static int seirAnazit(Book[] myBook, int key){        
            int pos = -20;
            for (int i=0; i<metritis; i++) {            
                if (myBook[i].getEtos()==key) {
                    pos = i;
                }  else pos = -1;
            }        
            return pos;
        }
       public static int diadAmazit(Book[] myBook, String key){
           int left = 0;
           int right = metritis-1;
           return diadAnazit2(myBook, key, left, right);
       }
       public static int diadAnazit2(Book[] myBook, String key, int left, int right){
         int mid,pos=-1;
         while(pos==-1 && left<=right){
           mid = (left+right)/2;
           if(myBook[mid].getIsbn().compareTo(key)>0){
             right = mid-1;
           } else if(myBook[mid].getIsbn().compareTo(key)<0){
             left = mid+1;
           } else {
             pos = mid;
           }
          }
          return pos;
       }
       public static int diadAmazit1(Book[] myBook, int key){
           int left = 0;
           int right = metritis-1;
           return diadAnazit2(myBook, key, left, right);
       }
       public static int diadAnazit2(Book[] myBook, int key, int left, int right){
         int mid,pos=-1;
         while(pos==-1 && left<=right){
           mid = (left+right)/2;
           if(myBook[mid].getEtos()>key){
             right = mid-1;
           } else if(myBook[mid].getEtos()<key){
             left = mid+1;
           } else {
             pos = mid;
           }
          }
          return pos;
       }
       public static void emfanise(Book[] myBook){
        for (int i=0; i<metritis; i++)
             System.out.println(myBook[i]);
            }
       public static void valueSearch(int min, int max,Book[] myBook){
          for(int i=0; i<metritis; i++){
           if(myBook[i].getTimi()>=min && myBook[i].getTimi()<=max )
    			System.out.println(myBook[i]);
    		}
        }    
       public static boolean elegxos10(long x, boolean z){
          long ar10 = (x%10)/(1)*1;
          long ar9 = (x%100)/(10)*2;
          long ar8 = (x%1000)/(100)*3;
          long ar7 = (x%10000)/(1000)*4;
          long ar6 = (x%100000)/(10000)*5;
          long ar5 = (x%1000000)/(100000)*6;
          long ar4 = (x%10000000)/(1000000)*7;
          long ar3 = (x%100000000)/(10000000)*8;
          long ar2 = (x%1000000000)/(100000000)*9;
          long ar1 = x/(1000000000)*10;
          long kfz = ar1+ar2+ar3+ar4+ar5+ar6+ar7+ar8+ar9+ar10;
          if(kfz%11==0) {
                return true;
          }       
                 return false;        
            }     
       public static boolean elegxos13(long x, boolean z){
        long ar13 = (x%10)/1*1;    
        long ar12 = (x%100)/10*3;
        long ar11 = (x%1000)/100*1;
        long ar10 = (x%10000)/1000*3;
        long ar9 = (x%100000)/10000*1;
        long ar8 = (x%1000000)/100000*3;
        long ar7 = (x%10000000)/1000000*1;
        long ar6 = (x%100000000)/10000000*3;
        long ar5 = (x%1000000000)/100000000*1;
        long ar4 = (x%10000000000L)/1000000000*3;
        long ar3 = (x%100000000000L)/10000000000L*1;
        long ar2 = (x%1000000000000L)/100000000000L*3;
        long ar1 = x/1000000000000L*1;
        long kfz = ar1+ar2+ar3+ar4+ar5+ar6+ar7+ar8+ar9+ar10+ar11+ar12+ar13;
        System.out.println(kfz);
        if(kfz%10==0) {
          return true;
          }               
        return false;
        } 
       public static void swap (Book myBook[], int x, int y) {
            Book temp = myBook[x];
            myBook[x] = myBook[y];
            myBook[y] = temp;
        }
       public static void BubbleSort(Book[] myBook, char a){
         int i;
         int j;
         boolean flag;
         if(a =='a'){
           for(i = 1; i<metritis; i++){
             flag = true;
             for(j=0; j<metritis-i; j++){
               if(myBook[j].getOnoma().compareToIgnoreCase(myBook[j+1].getOnoma())>0){
               swap(myBook, j, j+1);
               flag = false;
               }
             }
           }
         } else if(a  == 'b'){
           for(i = 1; i<metritis; i++){
             flag = true;
             for(j=0; j<metritis-i; j++){
               if(myBook[j].getIsbn().compareToIgnoreCase(myBook[j+1].getIsbn()) >0){
               swap(myBook, j, j+1);
               flag = false;
               }
             }
           }     
         } else if(a  == 'c'){
           for(i = 1; i<metritis; i++){
             flag = true;
             for(j=0; j<metritis-i; j++){
               if(myBook[j].getEtos()>myBook[j+1].getEtos()){
               swap(myBook, j, j+1);
               flag = false;
               }
             }
           }
     
         } else{
           for(i = 1; i<metritis; i++){
             flag = true;
             for(j=0; j<metritis-i; j++){
               if(myBook[j].getTimi()>myBook[j+1].getEtos()){
               swap(myBook, j, j+1);
               flag = false;
               }
             }
           }     
         }
       }
       public static void InsSort(Book[] myBook, char a){
         int position;
         Book key;
         if(a == 'a'){
           for(int i=1; i<myBook.length; i++){
             key = myBook[i];
             position = i;
             while(position>0 && myBook[position-1].getOnoma().compareToIgnoreCase(key.getOnoma())>0){
               myBook[position] = myBook[position-1];
               position--;
             }
           myBook[position] = key;  
           }
         }
       }
       public static void main(String args[]){
        int epilogi = 0;
        int pos;
        long pos2;
        Book[] myBook = new Book[10];
        do{
          ListaEpilogon();
          epilogi=UserInput.getInteger();
          switch(epilogi){
              case 1: EisagVivliou(myBook);
                  break;
              case 2: if(metritis>0){ EpilogiPedio();
              epilogi=UserInput.getInteger();
              switch(epilogi){
                  case 1: EpilogiMeth();
                  epilogi=UserInput.getInteger();
                  switch(epilogi){
                      case 1: 
                          System.out.println("Dose ISBN gia anazitisi");
                          String key = UserInput.getString();
                          pos2 = seirAnazit(myBook, key);
                          System.out.println(pos2);
                          break;
                      case 2:
                          System.out.println("Dose ISBN gia anazitisi");
                          key = UserInput.getString();
                          pos2 = diadAmazit(myBook, key);
                          System.out.println(pos2);
                          break;
                      case 3: break;
                  }
                      break;
                  case 2: EpilogiMeth();
                  epilogi=UserInput.getInteger();
                  switch(epilogi){
                      case 1: 
                          System.out.println("Dose Etsos gia anazitisi");
                          int key = UserInput.getInteger();
                          pos = seirAnazit(myBook, key);
                          System.out.println(pos);
                          break;
                      case 2:System.out.println("Dose Etos gia anazitisi");
                          key = UserInput.getInteger();
                          pos = diadAmazit1(myBook, key);
                          System.out.println(pos); 
                          break;
                      case 3: break;    
                 }
              } break;
              } else {
                System.out.println("\n ***Adeios pinakas*** \n");
                break;
              }
              case 3: if(metritis>0){
                  System.out.println("Dose thn mikroteri 8etiki timi");
                  int min = UserInput.getInteger();
                  System.out.println("Dose thn megaluteri 8etiki timi");
                  int max = UserInput.getInteger();
                  valueSearch(min, max, myBook);
                  break;} 
                     else {
                     System.out.println("\n ***Adeios pinakas*** \n");
                     break;}
     
              case 4: EpilogiTaks1();
              epilogi = UserInput.getInteger();
              switch(epilogi){
                  case 1: EpilogiTaks2();
                  epilogi = UserInput.getInteger();
                  switch(epilogi){
                      case 1: BubbleSort(myBook, 'a');
                      case 2: InsSort(myBook, 'a');
                      case 3:
                      case 4:
                      case 5:
                      case 6: break;                      
                  }
                      break;
                  case 2: EpilogiTaks2();
                  epilogi = UserInput.getInteger();
                  switch(epilogi){
                      case 1: BubbleSort(myBook, 'b'); 
                      case 2: InsSort(myBook, 'b');
                      case 3: 
                      case 4:
                      case 5:
                      case 6: break;
                  }
                      break;
                  case 3: EpilogiTaks2();
                  epilogi = UserInput.getInteger();
                  switch(epilogi){
                      case 1: BubbleSort(myBook, 'c');
                      case 2: InsSort(myBook, 'c');
                      case 3:
                      case 4:
                      case 5:
                      case 6: break;
                  }
                      break;
                  case 4: EpilogiTaks2();
                  epilogi = UserInput.getInteger();
                  switch(epilogi){
                      case 1: BubbleSort(myBook, 'd');
                      case 2: InsSort(myBook, 'd');
                      case 3:
                      case 4:
                      case 5:
                      case 6: break;
                  }
                      break;
                  case 5: break;
              }
                  break;              
     
              case 5:if(metritis>0){ emfanise(myBook);
              System.out.println();
              break;} else {
              System.out.println("\n ***Adeios pinakas*** \n"); 
              break;
              }
              case 6:System.out.println("\n\t ... TELOS PROGRAMMATOS ...\n");
              break;
              default : System.out.println("\tLathos Epilogi!!!Eisagete ari8mo 1-6\n");
        }
        }while(epilogi!=6);
      }
    }

    Thanks a lot!!

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: java.lang.NullPointerException

    Try using a println to find out which variable(s) have a null value. Put the println on the line before the line that throws the exception. When you find the variable with a null value, back trace the program and see where it should have been initialized and initialize it.
    If you can not locate the problem with the printlns added, post the code with the printlns and your question.

  3. The Following User Says Thank You to jps For This Useful Post:

    nikomania (May 12th, 2013)

Similar Threads

  1. Problem with java.lang.NullPointerException
    By more_dread in forum Exceptions
    Replies: 11
    Last Post: November 21st, 2011, 06:42 AM
  2. [SOLVED] java.lang.NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 3rd, 2011, 10:39 AM
  3. java.lang.NullPointerException
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2011, 04:39 PM
  4. java.lang.nullPointerException
    By ridg18 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 25th, 2010, 03:52 PM
  5. java.lang.NullPointerException - Help
    By mds1256 in forum Exceptions
    Replies: 5
    Last Post: November 30th, 2009, 06:31 PM