Web Analytics Made Easy -
StatCounter ArrayList Problem (java.lang.NullPointerException) - CodingForum

Announcement

Collapse
No announcement yet.

ArrayList Problem (java.lang.NullPointerException)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ArrayList Problem (java.lang.NullPointerException)

    Code:
    package isbnchecker;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class ISBNReader {
        
        private ISBN isbn = new ISBN();
        private ArrayList<String> inISBN = new ArrayList<String>();
        private String fileName = "";
        private String next = "";
    
        public ISBNReader(){
            this.isbn = null;
            this.inISBN = null;
        }
    
        public void setFileName(String fileName){
            this.fileName = fileName;
        }
    
        public String getFileName(){
            return fileName;
        }
        
        public void setISBNNumbers(){
            try{
                FileReader fr = new FileReader(fileName);
                Scanner in = new Scanner(fr);
                
                while(in.hasNext())
     [COLOR="Red"]               [B]inISBN.add(in.next());  [/B][/COLOR] 
            }
            catch(FileNotFoundException ex){
                Logger.getLogger(ISBNReader.class.getName()).log(Level.SEVERE, null, ex);
            }
                
        }
        
        public ArrayList<String> getISBNNumbers(){
            return inISBN;
        }
    }
    Exception in thread "main" java.lang.NullPointerException
    at isbnchecker.ISBNReader.setISBNNumbers(ISBNReader.java:43)
    at isbnchecker.Main.main(Main.java:34)
    Java Result: 1

    Hey everyone, I am currently working on this programming assignment for my computer science class and am having some trouble with an arraylist in the code above (the line in red). I can't seem to figure out what is causing the error I keep receiving. Any suggestions?

    Thanks!

  • #2
    inISBN has been set to null in the constructor.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      Finishing the Project

      Thank you, I realized what i had done right after I posted that. However, I've run in to another problem within my program that I can't seem to figure out. I keep receiving the error below:

      Exception in thread "main" java.lang.NumberFormatException: For input string: "978-0-13-601722-6"
      at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
      at java.lang.Integer.parseInt(Integer.java:458)
      at java.lang.Integer.parseInt(Integer.java:499)
      at isbnchecker.ISBN.stringToNumbers(ISBN.java:49)
      at isbnchecker.ISBNReader.setISBNNumbers(ISBNReader.java:44)
      at isbnchecker.Main.main(Main.java:35)
      Java Result: 1
      BUILD SUCCESSFUL (total time: 0 seconds)

      The program is supposed to read in a text file of ISBN numbers and check if they are valid, and then print out the list of valid, or invalid numbers.
      Here are all my classes:

      Code:
      public class Main {
      
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              ISBN isbn = new ISBN();
              ISBNReader reader = new ISBNReader();
              ISBNProcessor processor = new ISBNProcessor(reader, isbn);
              ISBNPrinter printer = new ISBNPrinter(processor);
      
      
              Scanner scan = new Scanner(System.in);
              reader.setFileName("isbn.txt");
      
              reader.setISBNNumbers();
              processor.processISBNNumbers();
              System.out.println(reader.getISBNNumbers());
              printer.printISBNNumbers();
          }
      
      }
      
      package isbnchecker;
      
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.util.ArrayList;
      import java.util.Scanner;
      import java.util.logging.Level;
      import java.util.logging.Logger;
      
      public class ISBNReader {
          
          private ISBN isbn = new ISBN();
          private ArrayList<ISBN> inISBN = new ArrayList<ISBN>();
          private String fileName = "";
          private String next = "";
      
          public ISBNReader(){
          }
      
          public void setFileName(String fileName){
              this.fileName = fileName;
          }
      
          public String getFileName(){
              return fileName;
          }
          
          public void setISBNNumbers(){
              try{
                  FileReader fr = new FileReader(fileName);
                  Scanner in = new Scanner(fr);
                  while(in.hasNextLine())
                      next = in.next();
                      isbn.setISBNString(next);
                      isbn.stringToNumbers();
                      inISBN.add(isbn);
              }
              catch(FileNotFoundException ex){
                  Logger.getLogger(ISBNReader.class.getName()).log(Level.SEVERE, null, ex);
              }
                  
          }
          
          public ArrayList<ISBN> getISBNNumbers(){
              return inISBN;
          }
      }
      
      import java.util.ArrayList;
      
      public class ISBNProcessor {
          
          private ISBNReader reader = new ISBNReader();
          private ISBN isbn = new ISBN();
          private String numOnlyString;
          private ArrayList<ISBN> inISBN = new ArrayList<ISBN>();
          private ArrayList<ISBN> validISBNs = new ArrayList<ISBN>();
          private ArrayList<ISBN> invalidISBNs = new ArrayList<ISBN>();
          private boolean isNumOnly;
          private int[] isbnNums = new int[13];
          
          ISBNProcessor(ISBNReader reader, ISBN isbn){
              this.reader = reader;
              this.isbn = isbn;
              inISBN = reader.getISBNNumbers();
          }
          
          public void processISBNNumbers(){
              for(int i = 0; i <inISBN.size();i++){
                  isbnNums = inISBN.get(i).getISBNNumbers();
                  if(isbnNums[12] == (10 - ((isbnNums[0] + 3*isbnNums[1] +isbnNums[2] + 3*isbnNums[3] +
                                             isbnNums[4] + 3*isbnNums[5] +isbnNums[6] + 3*isbnNums[7] +
                                             isbnNums[8] + 3*isbnNums[9] +isbnNums[10] + 3*isbnNums[11])
                                             % 10) % 10)){
                      validISBNs.add(inISBN.get(i));
                  }
                  else{
                    invalidISBNs.add(inISBN.get(i));
                  }
              }
          }
      
          public ArrayList<ISBN> getValidISBNNumbers(){
              return validISBNs;
          }
      
          public ArrayList<ISBN> getInvalidISBNNumbers(){
              return invalidISBNs;
          }
      
      }
      
      public class ISBNPrinter {
          
          private ISBNReader reader = new ISBNReader();
          private ISBN isbn = new ISBN();
          private ISBNProcessor processor = new ISBNProcessor(reader, isbn);;
      
          ISBNPrinter(ISBNProcessor processor){
              this.processor = processor;
          }
      
          public void printISBNNumbers(){
              System.out.println("Valid");
              for(int i = 0; i < processor.getValidISBNNumbers().size(); i++){
                  System.out.println(processor.getValidISBNNumbers().get(i).getISBNString());
              }
      
              System.out.println("Invalid");
              for(int i = 0; i < processor.getInvalidISBNNumbers().size(); i++){
                  System.out.println(processor.getInvalidISBNNumbers().get(i).getISBNString());
              }
          }
      }
      
      public class ISBN {
      
          private int[] isbnNumbers = new int[13];
          private String isbnString;
      
      
          public ISBN(int[] isbn){
              this.isbnNumbers = isbn;
          }
      
          ISBN() {
          }
      
          public void setISBNNumbers(int[] isbn){
              this.isbnNumbers = isbn;
          }
      
          public int[] getISBNNumbers(){
              return this.isbnNumbers;
          }
      
          public void setISBNString(String isbnString){
              this.isbnString = isbnString;
          }
      
          public String getISBNString(){
              return this.isbnString;
          }
      
          public void stringToNumbers(){
              for(int i = 0; i < isbnString.length(); i++){
                  if(Integer.parseInt(isbnString.substring(i,i+1)) == 1 ||Integer.parseInt(isbnString.substring(i,i+1)) == 2 ||
                     Integer.parseInt(isbnString.substring(i,i+1)) == 3 ||Integer.parseInt(isbnString.substring(i,i+1)) == 4 ||
                     Integer.parseInt(isbnString.substring(i,i+1)) == 5 ||Integer.parseInt(isbnString.substring(i,i+1)) == 6 ||
                     Integer.parseInt(isbnString.substring(i,i+1)) == 7 ||Integer.parseInt(isbnString.substring(i,i+1)) == 8 ||
                     Integer.parseInt(isbnString.substring(i,i+1)) == 9 ||Integer.parseInt(isbnString.substring(i,i+1)) == 0){
      
                          isbnNumbers[i] = (int) Integer.parseInt(isbnString.substring(i));
                  }
                  else{
      
                  }
      
              }
          }
      }
      Any help on finishing this program would be greatly appreciated, as it is due at midnight tonight, and i am struggling at this point

      Thanks!

      Comment


      • #4
        Only took a very quick look but my guess is the way stringToNumbers() is written.

        Given this string 978-0-13-601722-6 you are trying to take each character in the string and convert to a int; one by one. However, Java doesn't doesn't know how to convert a "-" to an int and is throwing a numberFormatException.

        You'll need to figure out a way to handle this. Remove the "-"'s from the string, or check that for the character being a "-" and then not trying to convert to an int are a couple possibilities.

        Comment


        • #5
          Ok I think i solved that problem. I removed the stringToNumbers method in ISBN and changed the setISBNNumbers in ISBNReader, so it reads in the ISBN numbers in a second time but this time only as ints. The setISBNNumbers now looks like this:

          Code:
              public void setISBNNumbers(){
                  try{
                      FileReader fr = new FileReader(fileName);
                      Scanner in = new Scanner(fr);
                      while(in.hasNextLine()){
                          next = in.next();
                          isbn.setISBNString(next);
                          inISBN.add(isbn);
                      }
                      in.close();
                      Scanner scanNums = new Scanner(fr);
                      while(scanNums.hasNextLine()){
                          for(int i = 0; i < 13; i++){
                              isbnNums[i] = scanNums.nextInt();
                          }
                      }
                      scanNums.close();
                  }
                  catch(FileNotFoundException ex){
                      Logger.getLogger(ISBNReader.class.getName()).log(Level.SEVERE, null, ex);
                  }      
              }
          However, I now receive a result that only reports the final ISBN number in the invalid list. The results look like this:
          Valid
          Invalid
          978-0-13-601722-6
          978-0-13-601722-6
          978-0-13-601722-6
          978-0-13-601722-6
          978-0-13-601722-6
          978-0-13-601722-6

          The text file contains the following:
          978-04-2135-828-8
          978-03-2135-828-8
          97A-03-2135-828-8
          978-1408819899
          978-0-13-216675-1
          978-0-13-601722-6

          Any suggestions?

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎