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. (DataSet.java:16) at wine.main.main(main.java:9) C:UsersDenisAppDataLocalNetBeansCache8.2executor-snippets un.xml:53: Java returned:
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.
at wine.main.main(main.java:9)
C:\Users\Denis\AppData\Local\NetBeans\Cache\8.2\executor-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 for(int j=1;j 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); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started