Question

Hi im getting a Exception in thread "main" java.lang.NullPointerException. What is wrong? Here is my code....

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);

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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&&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);
    }
}
Add a comment
Know the answer?
Add Answer to:
Hi im getting a Exception in thread "main" java.lang.NullPointerException. What is wrong? Here is my code....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)   ...

    Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)    at InfixExpression.Evaluate(InfixExpression.java:65)    at InfixExpression.setWholeExpr(InfixExpression.java:24)    at InfixExpression.<init>(InfixExpression.java:17)    at Main.testHW1(Main.java:17)    at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket....

    The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket. Socket are auto-closeable and thus can use a try-with-resources instead. Edit the dictionary program to use try-with-resources instead. package MySockets; import java.net.*; import java.io.*; public class DictionaryClient {       private static final String SERVER = "dict.org";    private static final int PORT = 2628;    private static final int TIMEOUT = 15000;    public static void main(String[] args) {        Socket   ...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

  • I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation...

    I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.println("Welcome to this Odd program!");        int userInput1 = input.nextInt();    }       public...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the...

    please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following        1) print out the original data in the file without doing anything to it.        2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT