1

I made a 2d array chart that shows and calculates the average noise levels of each car model as my school project. Now I need to have a separate file to store my array values and put them in the chart. I've tried to add the values in using a while loop but it gives me this error. How can I add values from my text file to the array? I am still new to java.

public static void writetoarray(){
 try{
    k = new Scanner(new File("test.txt"));

 }
catch(Exception e){
System.out.println("You got an error");
}
 int i = 0;
 while(k.hasNextInt()){
    firstarray[0][i] = k.nextInt();
    firstarray[1][i] = k.nextInt();
    firstarray[2][i] = k.nextInt();
    firstarray[3][i] = k.nextInt();
    firstarray[4][i] = k.nextInt();
    firstarray[5][i] = k.nextInt();
    firstarray[6][i] = k.nextInt();
    i++;
    }
  }


"Exception in thread "main" java.lang.NullPointerException
 at practice.writetoarray(practice.java:29)
 at practice.main(practice.java:11)"

This is my whole code:

import java.io.File;
import java.util.*;
public class practice {
public static Scanner k;
public int foo = Integer.parseInt(k.next());    
public static int n = 1;
public static int firstarray[][] ={{81,90,92,103,111,121,132,0},
{78,85,90,99,104,111,118,0},{80,86,91,95,100,108,119,0},
{87,90,95,101,111,121,133,0},{66,70,76,86,96,115,125,0},
{81,83,85,93,102,113,122,0},{76,78,80,85,94,104,114,0},{0,0,0,0,0,0,0,0}};
public static int y = 0;    
public static void main(String[] args) {
    String tableheadings[][]={{"\t\t\t\t    MPH"+"                              
|"},{"Model|","20","30","40","50","60","70","80","Avg     |",},
{"_____________________________________
___________________________________|"}};


writetoarray();
displayheader(tableheadings);
countArraytotal(firstarray);
arrayavg(firstarray);
arrayavgc(firstarray);
printrows(firstarray);
countRowandCol(firstarray);
}


 public static void writetoarray(){
 try{
    k = new Scanner(new File("test.txt"));

 }
catch(Exception e){
System.out.println("You got an error");
}
 int i = 0;
 while(k.hasNextInt()){
    firstarray[0][i] = k.nextInt();
    firstarray[1][i] = k.nextInt();
    firstarray[2][i] = k.nextInt();
    firstarray[3][i] = k.nextInt();
    firstarray[4][i] = k.nextInt();
    firstarray[5][i] = k.nextInt();
    firstarray[6][i] = k.nextInt();
    i++;
   }

 }



 public static void displayheader(String a[][]){
    for (int row=0 ;row<a.length;row++){
        for(int column=0; column<a[row].length;column++){
        System.out.print(a[row][column]+"\t");
        }
    System.out.println();
}
}

public static void printrows(int a[][]){

for (int row=0 ;row<a.length;row++){
    if(row<a.length-1){
    System.out.print(n+++"    |\t");
    }
    else{System.out.print("Avg  |\t");}
    for(int column=0; column<a[row].length;column++){

        System.out.print(a[row][column]+"\t");

    }

    System.out.print("|");
    System.out.println();
 }
}

public static void arrayavg(int[][] array) {
int s=   0;
for (int i = 0; i < array.length; i++){ s=0;
for (int j = 0; j < array[i].length; j++){             
s += array[i][j];
}
firstarray[i][7] = Math.round(s/(array.length-1));  
 }
}

public static void arrayavgc(int[][] array) {
int s=   0;

for (int i = 0; i < array.length; i++){ s=0;
for (int j = 0; j < array[i].length; j++){             
s += array[j][i];
}
firstarray[7][i] = Math.round(s/(array[i].length-1));
}
}

public static void countArraytotal(int[][] table){
int total=0;

for (int row=0;row<table.length;row++)
for (int col=0;col<table[0].length;col++)
  total += table[row][col];
firstarray[7][7]=Math.round(total/49);
}

public static void countRowandCol(int[][] array){
int rowsize = array.length;
int columnSize = array.length;
System.out.println("There are " + rowsize + " rows and " + columnSize+ " 
columns in this table.");

}
}

Also, ignore the values I already have in my array.

  • Possible duplicate of [Java serialization of multidimensional array](https://stackoverflow.com/questions/1467193/java-serialization-of-multidimensional-array) – EJoshuaS - Stand with Ukraine May 31 '17 at 18:56
  • 1
    What is the text in the `test.txt` Share the file. – Hamza Anis May 31 '17 at 18:57
  • It looks like the value in the file is more than the size of the array i.e why you are getting `NullPointerException`. – Hamza Anis May 31 '17 at 18:59
  • Other possible duplicate: [What is a NullPointerException and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – EJoshuaS - Stand with Ukraine May 31 '17 at 19:00
  • in the test.txt is just a couple numbers to test the program: 1 2 3 4 5 6 7 – urmomscarrot Jun 01 '17 at 18:00
  • I've solved the NullPointer exception but k.nextInt() is still giving me an error. `Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at practice.writetoarray(practice.java:42) at practice.main(practice.java:12)` – urmomscarrot Jun 01 '17 at 18:22

1 Answers1

0

One problem I notice is here:

try{
    k = new Scanner(new File("test.txt"));
 }
catch(Exception e){
    // You really should print out the full exception details here
    // It'll make debugging much easier
    System.out.println("You got an error");
}

// If you keep going after an exception here, you'll get a NullPointerException
// because k isn't initialized validly
int i = 0;
while(k.hasNextInt()){
    firstarray[0][i] = k.nextInt();
    firstarray[1][i] = k.nextInt();
    firstarray[2][i] = k.nextInt();
    firstarray[3][i] = k.nextInt();
    firstarray[4][i] = k.nextInt();
    firstarray[5][i] = k.nextInt();
    firstarray[6][i] = k.nextInt();
    i++;
}

If there's an exception when you try to initialize k, then you can't use it because it won't actually have been created.

Also, rather than printing out "You got an error" in the case when there's an exception, I strongly suggest printing out the full exception details (e.toString() will print out the exception message and stack trace). Obviously, you wouldn't want to display that to users in an actual production system, but for learning purposes it makes debugging a lot easier if you display (or at least log) the full details. (In production systems, we usually create a log of some kind with the full details of an exception like this so that we can debug more easily).