Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why am i getting NullPointerException in this class? Error: Exception in thread

ID: 3702144 • Letter: W

Question

Why am i getting NullPointerException in this class?

Error:

Exception in thread "main" java.lang.NullPointerException

at wine.DataSet.getDataSetSize(DataSet.java:37)

at wine.DataSet.<init>(DataSet.java:16)

at wine.main.main(main.java:9)

C:UsersDenisAppDataLocalNetBeansCache8.2executor-snippets un.xml:53: Java returned: 1

BUILD FAILED (total time: 0 seconds)

Code:

package wine;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public final class DataSet {

private DataSample[] dataArray;
public int samples;
public String[] sampleArr;
public int label;
  
public DataSet(String filename)throws FileNotFoundException{
System.out.println("Calculating number of lines...");
getDataSetSize(filename);
DataSample[] dataArray = new DataSample[samples];
double[] attributes=new double[samples]; //to convert string array into doubles array

for(int i=0;i<samples;i++){
String[] token=sampleArr[i].split(","); //this is to split string by ','
label=Integer.parseInt(token[0]); //split label is token[0]

for(int j=1;j<token.length;j++)
attributes[j-1]=Double.parseDouble(token[j]); //token[1] to token[13] are attributes, this will convert string into //double

dataArray[i]=new DataSample(label,attributes);
}
System.out.println(dataArray[0]);
}
  
public int getDataSetSize(String filename) throws FileNotFoundException {
samples=0;
File file = new File(filename);
try (Scanner input = new Scanner(file)) {
while (input.hasNextLine()){
sampleArr[samples]=input.nextLine(); //sampleArr , has each line read from file
samples++;
}
input.close();
System.out.println("done ");
}
return samples;
}
  
public double[] getMean(int label){
double x[] = null;
return x;
}
  
public double[] getStd(int label){
return null;   
}
  
public DataSample[] getDataSet(){
return dataArray;   
}
}

main:


package wine;

import java.io.FileNotFoundException;
import java.util.Arrays;

public class main {
public static void main(String[] args) throws FileNotFoundException {
DataSet train = new DataSet("wine.txt");
System.out.println(train.samples);
}
}

Explanation / Answer

Hi,

in your code your using globle variable

public String[] sampleArr;

Here you just declare sampleArr is an Array of Strings, actually memory is not allocated. So sampleArr is null, it's throwing NULL pointer exception.

So you need to allocate memory for that variable by creating object somewhere (i think in DataSet method ).like this

sampleArr = new String[<size>];

size you need to provide based on your data otherwise first count number of lines in your file and assign that count size.

Add the following lines to your getDataSetSize method

try (Scanner input = new Scanner(file))

{

int count = 0;

while (input.hasNextLine())

{

count++;

}

sampleArr = new String[count];

input.close();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote