java.lang.NullPointerException after serializing

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 444 Views - Last Post: 28 October 2020 - 12:32 PM Rate Topic: -----

#1 yjay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 20

java.lang.NullPointerException after serializing

Posted 27 October 2020 - 05:12 PM

I have two java files, one with machine learning models and the other with a dataframe to predict the outcome but I keep getting this error:
Exception in thread "main" java.lang.NullPointerException


The model.java file looks like:
RandomForest rfr = RandomForest.fit(Formula.lhs("height"), data);
DataFrame predict = dtFrameTest.drop("height",);
int[] rfpredict = rfr.predict(predict);

try {
	 FileOutputStream fileOut =
	 new FileOutputStream("./src/rfrSerialize.ser");
	 ObjectOutputStream out = new ObjectOutputStream(fileOut);
	 out.writeObject(rfr);
	 out.close();
	 fileOut.close();
	 System.out.printf("Serialized data is saved in ./src/rfrSerialize.ser");
	 } catch (IOException i) {
	    i.printStackTrace();
	   }
	}



and predict.java looks like:
RandomForest rfr;
  	     try {
  	         FileInputStream fileIn = new FileInputStream("./src/rfrSerialize.ser");
  	         ObjectInputStream in = new ObjectInputStream(fileIn);
  	         rfr = (RandomForest) in.readObject();
  	         in.close();
  	         fileIn.close();
  	      } catch (IOException i) {
  	         i.printStackTrace();
  	         return;
  	      } catch (ClassNotFoundException c) {
  	         System.out.println("RandomForest class not found");
  	         c.printStackTrace();
  	         return;
  	      }

  	   int[] rfpredict = rfr.predict(myDataFrame);
  	   System.out.println(rfpredict);
	 }



Here is the stack trace:
Exception in thread "main" java.lang.NullPointerException
	at smile.data.type.StructType.fieldIndex(StructType.java:81)
	at smile.data.formula.Variable$1.<init>(Variable.java:80)
	at smile.data.formula.Variable.bind(Variable.java:78)
	at smile.data.formula.Formula.lambda$bind$12(Formula.java:253)
	at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:271)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
	at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
	at smile.data.formula.Formula.bind(Formula.java:254)
	at smile.classification.DataFrameClassifier.predict(DataFrameClassifier.java:48)
	at Predict.computeTheFile(Predict.java:397)



Predict.java:397 contains int[] rfpredict = rfr.predict(myDataFrame);

Thanks so much!

Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.NullPointerException after serializing

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 6,521
  • Joined: 25-December 13

Re: java.lang.NullPointerException after serializing

Posted 27 October 2020 - 05:51 PM

Quote

Exception in thread "main" java.lang.NullPointerException
at smile.data.type.StructType.fieldIndex(StructType.java:81)

Is line 81 in your code? What variable on that line has the null value?
Was This Post Helpful? 0
  • +
  • -

#3 yjay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 20

Re: java.lang.NullPointerException after serializing

Posted 27 October 2020 - 06:05 PM

View PostNormR, on 27 October 2020 - 05:51 PM, said:

Quote

Exception in thread "main" java.lang.NullPointerException
at smile.data.type.StructType.fieldIndex(StructType.java:81)

Is line 81 in your code? What variable on that line has the null value?


Line 81 in StructType.java is from the smile library:
    /** Returns the index of a field. */
    public int fieldIndex(String field) {
        return index.get(field);
    }

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 6,521
  • Joined: 25-December 13

Re: java.lang.NullPointerException after serializing

Posted 27 October 2020 - 06:08 PM

You have posted 4 source lines. I am assuming line 3 is line 81.
What are the values of the variables on line 81 when it is executed?
Was This Post Helpful? 0
  • +
  • -

#5 yjay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 20

Re: java.lang.NullPointerException after serializing

Posted 27 October 2020 - 06:16 PM

View PostNormR, on 27 October 2020 - 06:08 PM, said:

You have posted 4 source lines. I am assuming line 3 is line 81.
What are the values of the variables on line 81 when it is executed?

Sorry yes line 81 is line 3
return index.get(field);


When I first go through myDataFrame it shows each column as the field but when it goes through the try/catch block of deserialization then it throws IOException and shows field as "null"
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 6,521
  • Joined: 25-December 13

Re: java.lang.NullPointerException after serializing

Posted 27 October 2020 - 06:43 PM

Quote

shows field as "null"

Can you backtrack in the code to see where field is given a value and check if that value is null?


Quote

it throws IOException

What statement is doing that?
Was This Post Helpful? 0
  • +
  • -

#7 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3729
  • View blog
  • Posts: 17,048
  • Joined: 20-September 08

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 01:57 AM

Quote

Here is the stack trace:

But not the full trace. Can you post it please?
Was This Post Helpful? 0
  • +
  • -

#8 yjay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 20

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 07:21 AM

View PostNormR, on 27 October 2020 - 06:43 PM, said:

Quote

shows field as "null"

Can you backtrack in the code to see where field is given a value and check if that value is null?

So it seems that when I create a DataFrame:
DataFrame myDataFrame = computeTheFile(myFileName)

it then goes into StructType.java and "creates a struct data type":
    /** Creates a struct data type. */
    public static StructType struct(StructField... fields) {
        return new StructType(fields);
    }


at that point, fields is StructField[3], opening that shows name as equal to the name of the columns.
Then it goes into:
    /** Returns the index of a field. */
    public int fieldIndex(String field) {
        return index.get(field);
    }

and field is the name of the column, it goes through each column, making the column name the value of field.
Then it returns the dataframe.
Next it goes into the try/catch block of deserialization, when it reaches the catch it shows field as "null":
in.close();
fileIn.close();
} catch (IOException i) {


However opening field there are values:
110, 117, 108, 108.

Quote

Quote

it throws IOException

What statement is doing that?

This is in the deserializing try/catch after trying to return the index of the field:
try {
    FileInputStream fileIn = new FileInputStream("./src/rfrSerialize.ser");
    ObjectInputStream in = new ObjectInputStream(fileIn);
    rfr = (RandomForest) in.readObject();
    in.close();
    fileIn.close();
    } catch (IOException i) {
      i.printStackTrace();
      return;


Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 6,521
  • Joined: 25-December 13

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 07:34 AM

What was in the stack trace for that exception?

If the method executes the return statement on line 9,
How does it execute the statement on line 17 in post#1?

Your description of what happened is incomplete or incorrect. It does not make sense that there was an exception that caused the method to return and that a later statement in that same method was executed.
Was This Post Helpful? 0
  • +
  • -

#10 yjay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 20

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 07:38 AM

View Postg00se, on 28 October 2020 - 01:57 AM, said:

Quote

Here is the stack trace:

But not the full trace. Can you post it please?

This is all I seem to get when the exception occured
Exception in thread "main" java.lang.NullPointerException
	at smile.data.type.StructType.fieldIndex(StructType.java:81)
	at smile.data.formula.Variable$1.<init>(Variable.java:80)
	at smile.data.formula.Variable.bind(Variable.java:78)
	at smile.data.formula.Formula.lambda$bind$12(Formula.java:253)
	at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:271)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
	at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
	at smile.data.formula.Formula.bind(Formula.java:254)
	at smile.classification.DataFrameClassifier.predict(DataFrameClassifier.java:48)
	at Predict.main(Predict.java:86)


Was This Post Helpful? 0
  • +
  • -

#11 yjay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 20

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 08:03 AM

View PostNormR, on 28 October 2020 - 07:34 AM, said:

What was in the stack trace for that exception?

If the method executes the return statement on line 9,
How does it execute the statement on line 17 in post#1?

Your description of what happened is incomplete or incorrect. It does not make sense that there was an exception that caused the method to return and that a later statement in that same method was executed.


Sorry I put some more print statements and seems the catch IO Exception is not where the trace is from, it runs the try/catch then exits and continues until
int[] rfpredict = rfr.predict(myDataSet);
and then prints the error and stack trace.
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 6,521
  • Joined: 25-December 13

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 08:12 AM

Please post the current code that shows where the IOException is caught and where the call is made that eventually causes the NPE.

If there is a problem loading the class's data, why should the code continue trying to execute it?
Was This Post Helpful? 0
  • +
  • -

#13 yjay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 20

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 08:25 AM

View PostNormR, on 28 October 2020 - 08:12 AM, said:

Please post the current code that shows where the IOException is caught and where the call is made that eventually causes the NPE.

DataFrame myDataFrame = computeTheFile(myFileName);
RandomForest rfr;
try {
	FileInputStream fileIn = new FileInputStream("./src/rfrSerialize.ser");
	ObjectInputStream in = new ObjectInputStream(fileIn);
	rfr = (RandomForest) in.readObject();
	in.close();
	fileIn.close();
	} catch (IOException i) {
	 i.printStackTrace();
	 return;
	} catch (ClassNotFoundException c) {
	  System.out.println("RandomForest class not found");
	  c.printStackTrace();
	  return;

       int[] rfpredict = rfr.predict(myDataFrame);
       System.out.println(rfpredict);



Quote

If there is a problem loading the class's data, why should the code continue trying to execute it?


I don't think the IO Exception ever runs, I think the try/catch exits after the try and then continues with trying to predict.
Was This Post Helpful? 0
  • +
  • -

#14 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3729
  • View blog
  • Posts: 17,048
  • Joined: 20-September 08

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 08:45 AM

Quote

... it runs the try/catch then exits and continues until ...

So is the line you quoted after that sentence line 86 (below)?

Quote

at Predict.main(Predict.java:86)

This post has been edited by g00se: 28 October 2020 - 08:45 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#15 yjay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 29-September 20

Re: java.lang.NullPointerException after serializing

Posted 28 October 2020 - 08:50 AM

View Postg00se, on 28 October 2020 - 08:45 AM, said:

Quote

... it runs the try/catch then exits and continues until ...

So is the line you quoted after that sentence line 86 (below)?

Quote

at Predict.main(Predict.java:86)


int[] rfpredict = rfr.predict(myDataFrame);

Is line 86 in Predict.java
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2