1

Given a text file as follow :
10
100
99
99
96
96
92
92
91
88
87
86

Where the first number "10" means that the text file is containing 10 integer and the second number 100 means that all the numbers in the text file doesn't exceed 100.
My objective is to read the text and fill an int data[][]; as follow :

data[0][0]=1    data[0][1]=99
data[1][0]=2    data[1][1]=99
data[2][0]=3    data[2][1]=96
data[3][0]=4    data[3][1]=96
data[4][0]=5    data[4][1]=92
data[5][0]=6    data[5][1]=92
data[6][0]=7    data[6][1]=91
data[7][0]=8    data[7][1]=88
data[8][0]=9    data[8][1]=87
data[9][0]=10   data[9][1]=86
cbin = 100 // bin capacity 
nbin = 10 // number of objects 

That's means first raw for index and second for item's value or weights .. and int cbin = \\the second text's value and int nbin = \\the first text's value
the problem that i get an Exception in thread

"AWT-EventQueue-0" java.lang.NullPointerException


Here is my code :

 public static int[][] data=null; \\ in the first of the document

       JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f= chooser.getSelectedFile();

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(f));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
        }
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        int i=0,j=0;
        while (line != null) {

            if(i==0){
                nbin= (int) Integer.parseInt(""+line);
                System.out.println(nbin);
            }
            else if (i==1) {
                cbin=(int) Integer.parseInt(""+line);
                System.out.println(cbin);
            }
          //  sb.append(line);
            line = br.readLine();
        if(i >= 2 && line != null){

               data[j][1]=(int) Integer.parseInt(line);
               data[j][0]=j;
               j++;

             }

            i++;

        }
          }   catch (IOException ex) {
            Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
        try {
            br.close();
        } catch (IOException ex) {
            Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

Any ideas ??

13
  • Where are you using awt here? Commented Sep 13, 2013 at 23:24
  • 1
    I have an idea: read the stack trace of your exception (which you haven't posted), see in which line it occurs, and then find out how any variable can be null on that line. Commented Sep 13, 2013 at 23:24
  • @hexafraction am using java swing and the algorithm already print for me the nbin and cbin correctly then it stop. Besides the f file refer to the text i mentioned above . Commented Sep 13, 2013 at 23:28
  • are you actually initializing you array somewhere? Commented Sep 13, 2013 at 23:33
  • @Gianmarco yes of course i already edited the code and mentioned it Commented Sep 13, 2013 at 23:36

2 Answers 2

1

You are not initializing the array!!

public static int[][] data=null;

This is not properly initializing, you are "declaring" the variable. That's the same as:

public static int[][] data;

No differences.

If you want o be able to add something in your array you have to initialize it:

This is the example:

public static int[][] data;
try{
    data[1][1] = 5;
    System.out.println("Added");
}catch(NullPointerException e){
    System.out.println("First exception catched");
}
data = new int[10][10];
try{
    data[1][1] = 5;
    System.out.println("added at the second try");
}catch(NullPointerException e){
    System.out.println("Second Exception catched");
}

this code will give as result:

First exception catched
added at the second try

I how you understood why

Sign up to request clarification or add additional context in comments.

Comments

0

Using java.util.Scanner, statements such as Scanner scanner = new Scanner(System.in);, while (scanner.hasNext()) { /* ... */ }, and int n = scanner.nextInt(); will make it a much simpler program.

Like so:

import java.util.Scanner;

public class Read {
  public static void main (String [] args) {
    try (
      Scanner scanner = new Scanner(System.in);
    ) {
      int nbin = scanner.nextInt();
      int cbin = scanner.nextInt();
      int data[][] = new int[nbin][2];
      for (int i = 0; i < nbin; ++i) {
        data[i][0] = i + 1;
        data[i][1] = scanner.nextInt();
      }
    }
  }
}

2 Comments

As you said @Gianmarco we need to initialize the array before filling it !! so am editing your code @no_answer_not_upvoted and then it works !! thnx for the help guys and in fact using scanner is so much better and easier
you inverted the data :p data[i][0] = i+1; data[i][1] = scanner.nextInt();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.