Parse string into double

Go To StackoverFlow.com

0

My code doesn't seem to work when the string token is an int. Here it is:

public class CO2Data {

    CO2Data dataSet[] = new CO2Data[10];
    String strLine = "Italy 476.08  116.86  2   592";
    int lines = 10;
    double roadCO2;

    public void saveLineInfo(String strLine, int lines) {
        StringTokenizer token = new StringTokenizer(strLine);
        String str = "hello";
        int count = 0;
        for (int i = 0; i < lines; i++) {
            while (token.hasMoreTokens()) {
                str = token.nextToken();
                if (count == 3) {
                    getRoadCO2(str, roadCO2);
                    dataSet[i].setRoadCO2(roadCO2);
                }
                count++;
            }
        }
    }

    public double getRoadCO2(String str, double roadCO2) {
        roadCO2 = Double.parseDouble(str);
        return roadCO2;
    }

    public void setRoadCO2(double roadCO2) {
        this.roadCO2 = roadCO2;
    }
}

In the rest of the lines, roadCO2 is a double, so I'm guessing my program is getting confused? How do I fix it? Thanks so much!

2012-04-04 03:54
by NoName
You may want to add a tag for this for the actual language you are using. It looks like Java, but it could be one of may different languages. Also, the scoping for roadCO2 is a mess within the getRoadCO2 function.. - Ariel 2012-04-04 03:57
Is this C# or C++?? Also is your question concerning the variable roadCO2 or does it concern the parameter "String strLine" - hypervisor666 2012-04-04 03:59
Sorry, it's java - NoName 2012-04-04 04:00
What is the error you're getting? Please post a sample input and the actual/desired output - Chetter Hummin 2012-04-04 04:00
USA 5951.13 1530.3 5.16 777 UK 2573.4 119.68 1.99 470 Error: null Exception in thread "main" java.lang.NullPointerException at CO2Data.main(CO2Data.java:56) *Line 56: System.out.println(dataSet[1].country); (the data at the beginning is the 2 lines that work being printed before it crashes on Italy - NoName 2012-04-04 04:05
There is no way to tell what is causing NullPointerException at line 56 of your original program (possibly the system out println) looking at the code you have given us. Only reasonable explanation is that dataSet[1] is null - ring bearer 2012-04-04 04:09


2

You are getting NullPointerException because,

You've declared an Array of CO2Data dataSet[] = new CO2Data[10];, but every element inside this CO2Data[] array is pointing to Null.

Hence, this call: dataSet[i].setRoadCO2(roadCO2); will generate a NullPointerException because dataSet[i] is pointing to null.

Solution :

Instantiate dataSet[i] = new CO2Data(); then call dataSet[i].setRoadCO2(roadCO2);

2012-04-04 05:09
by Chethan kumar


1

I'd recommend changing the names of the parameters to your methods to something slightly different than the class datamember "roadCO2". That might help you sort out the error :)

2012-04-04 04:04
by hypervisor666


1

When I ran your code, I got a NullPointerException at line 22. This is beacuse the array 'data' has not been initialized.

You can initialize your array as follows

for(int i = 0; i < dataSet.length; i++) {
    dataSet[i] = new CO2Data();
}
2012-04-04 04:06
by Chetter Hummin
Ads