-1

I have a Main class:

public class Retrigger {
    public static void main(String[] args){
        Long i= 97944605L;
        com.armus.flow.Implement rdf = new com.armus.flow.Implement();
        try {
            rdf.retrfail(i);
        }
        catch(Throwable e){
            System.out.println("In exception a = "+e+" "+i);
            e.printStackTrace();
            return;            
        }
    }
}

I am calling method retrfail of the Implement class and passing a long value:

import com.armus.common.Dsessionservice;

public class Implement
    extends Remote
    implements DMSer, Ajaxser {

    private Dsessionservice flowservice;

    private Dsession getDsession(long sessionId)
      throws ServiceException {
      try {
          dss = this.flowservice.getprocessname(Long.valueOf(sessionId));
      }
      catch (ServerException e) {
          //some code
      }
      //some code
    }

    public void retrfail(long sessionId) {
        Dsession dss = getDsession(sessionId);
        // some code
    }
}

The implementing class passes the id to other Dsessionservice interface to get the process name.

public abstract interface Dsessionservice 
{
  public abstract Dsessionservice getprocessname(Long paramLong)
    throws ServerException;
  }

The program compiles fine. But I am getting java.lang.nullpointerexception when running the program at the below line

dss = this.flowservice.getprocessname(Long.valueOf(sessionId));

What am I doing wrong here.

Can someone please help?

Markus Pscheidt
  • 6,853
  • 5
  • 55
  • 76

1 Answers1

0

You forgot to initialize your flowserive variable.

In Java, when you declare a variable like private Dsession flowserive; it is not initialized to anything, and therefore has no member method getprocessname(...) Trying to access this method when it does not exist throws a java.lang.nullpointerexception.

Try something like this :

import com.armus.common.Dsession;

public class Implement
  extends Remote
  implements DMSer, Ajaxser
{
  private Dsession flowserive;

// ADDING CONSTRUCTOR HERE ////
public Implement() {
    this.flowservice = new Dsession(); // Or initialize with any parameters you need
}
///////////////////////////////


  private Dsession getDsession(long sessionId)
    throws ServiceException
  {
    try
    {
      dss = this.flowserive.getprocessname(Long.valueOf(sessionId));
    }
    catch (ServerException e)
    {
      //some code
    }
   //some code

   public void retrfail(long sessionId)
  {
     Dsession dss = getDsession(sessionId);
    // some code
  }
}

I'm sorry I do not know your Dsession class, so maybe you need to change this to initialize the Dsession object correctly...

Vincent
  • 185
  • 1
  • 11
  • "it is not initialized to anything", false it is initialize. It's of type [NullType](https://docs.oracle.com/javase/7/docs/api/javax/lang/model/type/NullType.html), with the value `null`. But this does not have any method, so when you try to call a method on it, it throws a NPE. – vincrichaud Jun 21 '18 at 08:24
  • Dsession is other class which is present in other class file...so I think No need to initialise it..otherwise I would have got error on compilation...It is compiled successfully....This is run time error I am getting...Thank you for your suggestions...Waiting for more suggestions – Abhay Sontakke Jun 21 '18 at 08:59
  • Yo DO need to initialize it if you want to call a member method of it. There is no way around it in Java. If you get a compilation problem, then the problem is not that you initialize it, but that you are initializing it wrong. Do you have a documentation for Dsession, which you could refer to ? – Vincent Jun 21 '18 at 09:19
  • Hi,....I have updated the question... – Abhay Sontakke Jun 22 '18 at 03:08
  • I would still give the same answer. Changing to an interface doesn't change anything. However please note that interfaces *cannot be instantiated*, so you must instantiate with a class that implements your interface. – Vincent Jun 22 '18 at 06:56