0

I wrote a project A which contains a class KMeans.java. The class contains a public local varaible iteration. I build the project to a jar and add the jar to my spring boot web project B.

/**
ProjectA.jar
--KMeans.java
*/
public class KMeans{
  public int iteration;

  public void run(){
    for( iteration=0; iteration<1000; iteration++){
      ....
    }
  }
}

I try to obtain the iteration value in B to show it in web page dynamically. However, I dont know how to obtain the value. Thus, I decide to write the value into database.

I create a class ModelStatusUtils.java in A which contains save(int iteration) method.

  public abstract class ModelStatusUtils{
     public abstract void save(int iteration);
  }

And I put the class as a parameter in run() method in KMeans.java so that I can save the iteration which can be controlled by external class.

  ....
  public void run(ModelStatusUtils model){

     for( iteration=0; iteration<1000; iteration++){
      ....
      model.save(iteration);
     }
  }
  ...

Next, I create a class WebModelStatusUtils.java in B (I think the following error is because WebModelStatusUtils extends ModelStatusUtils in A, so spring boot cannot scan the package and could not autowired jpa correctly) that extends ModelStatusUtils.java . I used jpa and autowired the jpa in WebModelStatusUtils.java.

  public class WebModelStatusUtils extends ModelStatusUtils{
    @Autowired
    private ModelStatusRepository modelStatusRepository;

    @Override
    public int saveIteration(int i) {
      ModelStatus modelStatus = new ModelStatus(i);
      modelStatusRepository.save(modelStatus);
      return 1;
     }

  }

And repository is:

public interface ModelStatusRepository extends JpaRepository<ModelStatus, Long> {
}    

However, it calls error:

java.lang.NullPointerException: null, at WebModelStatusUtils .saveIteration(WebModelStatusUtils .java:23) ~[classes/:na]

My question is:

1) how to obtain instance's local variable of the external jar in web project

2) Why it throws null exception. How should i to solve it

DuFei
  • 447
  • 6
  • 20
  • `The class contains a public local variable iteration`. This is not a `local variable` but `instance variable` – Mehraj Malik Aug 20 '18 at 04:55
  • no. this is a web application and the class is implements from a external class @mrkernelpanic – DuFei Aug 20 '18 at 06:48
  • @DuFei prove that the reason for your NPE is not the one linked in the duplicate. Post the relevant code. The fact that it's a web app and that your class *is implements from a external class* (whatever that might mean) is completely irrelevant. – JB Nizet Aug 20 '18 at 06:53
  • @JBNizet Sorry, it is a duplicated problem – DuFei Aug 20 '18 at 07:31

0 Answers0