Hi im getting a Exception in thread \"main\" java.lang.NullPointerException. Wha
ID: 3734040 • Letter: H
Question
Hi im getting a Exception in thread "main" java.lang.NullPointerException. What is wrong?
Here is my code.
class MicrosoftMonarchs
{
//declaring all the arrays
public String array[];
public double close[];
public int volume[];
public double open[];
public double high[];
public double low[];
//declaring both size and totalFileData
public long totalFileData=0;
public int size=5;
//main file
public static void main(String[] args)
{
MicrosoftMonarchs data = new MicrosoftMonarchs();
//setting all the arrays equal to the size of the given text
data.array= new String[data.size];
data.close= new double[data.size];
data.volume= new int[data.size];
data.open= new double[data.size];
data.high= new double[data.size];
data.low= new double[data.size];
//read in the text file
data.readTextFile(data);
//print the data from the text file
data.printData(data);
//analyze the data from the text file
data.analyzeData(data);
}
//implementation of the read in from text file
public void readTextFile(MicrosoftMonarchs objdata)
{
//create data member i and set it to zero
int i=0;
BufferedReader reader;
//a try and catch block
try
{
//reading in the text file
reader = new BufferedReader(new FileReader("MicrosoftStockData.txt"));
String first=reader.readLine();
//while loop
while(first!=null)
{
//reading in the first line with commas in between each piece of data
first=reader.readLine();
String[] datas=first.split(",");
//assigning the data to the appropriate array
if(datas.length>0&&i<objdata.size)
{
objdata.array[i]=datas[0];
objdata.close[i]=Double.parseDouble(datas[1]);
objdata.volume[i]=Integer.parseInt(datas[2]);
objdata.open[i]=Double.parseDouble(datas[3]);
objdata.high[i]=Double.parseDouble(datas[4]);
objdata.low[i]=Double.parseDouble(datas[5]);
}
i++;
objdata.totalFileData++;
}
//stop reading in once there is nothing left from the text file
reader.close();
}
//generic catch block
catch(IOException e)
{
e.printStackTrace();
}
}
//implementation of the printData class
public void printData(MicrosoftMonarchs objdata)
{
//create data member i and set it to zero
int i=0;
//Printing out the data
System.out.println(objdata.totalFileData+"data lines read from 'MicrosoftStockData.txt'");
System.out.println("First 12 days of data ranging from " + objdata.array[0]+"-" +objdata.array[3]);
System.out.println("Date"+" "+"Close"+ " "+"Volume"+" "+"Open"+" "+"High"+" "+"Low/n");
//a for loop which prints out the data four times
for(i=0; i<4; i++)
{
System.out.print(objdata.array[i]+" "+objdata.close[i]+" "+objdata.volume[i]+" "+objdata.open[i]+" "+objdata.high[i]+" "+objdata.low[i]+"/n");
}
}
//implementation of the analyzeData class
public void analyzeData(MicrosoftMonarchs objdata)
{
//declare two variables for both high and low
double highData=objdata.close[0];
double lowData=objdata.close[0];
//declare the indexes
int indexA=0;
int indexB=0;
//
for(int i=1; i<objdata.close.length; i++)
{
if(objdata.close[i]> highData)
{
highData=objdata.close[i];
indexB=i;
}
if(objdata.close[i]< lowData)
{
lowData=objdata.close[i];
indexA=i;
}
}
System.out.println("Highest closing stock data date: "+objdata.array[indexB]+" and volume: "+objdata.volume[indexB]);
System.out.println("Lowest closing stock data date: "+objdata.array[indexA]+" and volume"+objdata.volume[indexA]);
double highDiff=objdata.high[0]-objdata.low[0];
indexB=0;
for(int i=1; i<objdata.close.length; i++)
{
if((objdata.high[i]-objdata.low[i])>highDiff)
{
highDiff=objdata.high[i]-objdata.low[i];
indexB=i;
}
}
System.out.println("Highest diffrence data: "+objdata.array[indexB]+" and value: "+highDiff);
}
}
Explanation / Answer
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; class MicrosoftMonarchs { //declaring all the arrays public String array[]; public double close[]; public int volume[]; public double open[]; public double high[]; public double low[]; //declaring both size and totalFileData public long totalFileData=0; public int size=5; //main file public static void main(String[] args) { MicrosoftMonarchs data = new MicrosoftMonarchs(); //setting all the arrays equal to the size of the given text data.array= new String[data.size]; data.close= new double[data.size]; data.volume= new int[data.size]; data.open= new double[data.size]; data.high= new double[data.size]; data.low= new double[data.size]; //read in the text file data.readTextFile(data); //print the data from the text file data.printData(data); //analyze the data from the text file data.analyzeData(data); } //implementation of the read in from text file public void readTextFile(MicrosoftMonarchs objdata) { //create data member i and set it to zero int i=0; BufferedReader reader; //a try and catch block try { //reading in the text file reader = new BufferedReader(new FileReader("MicrosoftStockData.txt")); String first=reader.readLine(); //while loop while((first=reader.readLine()) != null) // read first then check if it is null { //reading in the first line with commas in between each piece of data String[] datas=first.split(","); //assigning the data to the appropriate array if(datas.length>0&&iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.