NoKHe
NoKHe

Reputation: 51

how can i resolve this "Etat HTTP 500 - java.lang.NullPointerException"?

i'm developping a restful web service that extract data (messages) from my database and return all messages!

every single message is a MsgBean ( with an id, contenu, from, numexp,... )

the web service couldn't return a table so i created a new object that contain a table (msgTable) of MsgBean !

while running my web service on the rest console, i got this error :


Etat HTTP 500 - java.lang.NullPointerException

type Rapport d''exception

message java.lang.NullPointerException

description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.

Click to see the rest of error


my MsgBean is :

@XmlRootElement
public class MsgBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String frommm;
    private String contenu;
    private String DateEnvoi;
    private String NumExp;
    private int idu;

      public MsgBean(){};

    @XmlElement
      public int getIdu() {
        return idu;
    }

    public void setIdu(int idu) {
        this.idu = idu;
    }



    @XmlElement
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
 @XmlElement
    public String getFrommm() {
        return frommm;
    }

    public void setFrommm(String frommm) {
        this.frommm = frommm;
    }


 @XmlElement
    public String getContenu() {
        return contenu;
    }

    public void setContenu(String contenu) {
        this.contenu = contenu;
    }
 @XmlElement
    public String getDateEnvoi() {
        return DateEnvoi;
    }

    public void setDateEnvoi(String DateEnvoi) {
        this.DateEnvoi = DateEnvoi;
    }


 @XmlElement
    public String getNumExp() {
        return NumExp;
    }

    public void setNumExp(String NameExp) {
        this.NumExp = NameExp;
    }



}

my msgTabl is :

@XmlRootElement
public class msgTabl implements Serializable {

    String test;
    MsgBean[] m = new MsgBean[100];


   public msgTabl(){};

    @XmlElement
    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

   @XmlElement
    public MsgBean getM(int i) {
        return m[i];
    }

    public void setM(int i, MsgBean mb) {

        this.m[i].setId(mb.getId());
        this.m[i].setIdu(mb.getIdu());
        this.m[i].setFrommm(mb.getFrommm());
        this.m[i].setNumExp(mb.getNumExp());
        this.m[i].setDateEnvoi(mb.getDateEnvoi());
        this.m[i].setContenu(mb.getContenu());
    }



}

and here is the web service that i did test with :

@GET
    @Path("/historiquemethod")
    @Produces("application/json")
    //@Produces("text/plain")
    public msgTabl historique(
            @QueryParam("pseudo") String pseudo,
            @QueryParam("motDePasse") String motDePasse
    ) {


        msgTabl tab = new msgTabl();
        MsgBean ms = new MsgBean();

        // set information into our msg to test
        ms.setContenu("contenu");
        ms.setFrommm("8080");
        ms.setNumExp("2584126");
        ms.setId(1);
        ms.setIdu(2);
        ms.setDateEnvoi("date");

        // set the message into the first table case
        tab.setM(0, ms);
        tab.setTest("ok");

        return tab;
    }

Upvotes: 0

Views: 198

Answers (1)

Sikorski
Sikorski

Reputation: 2691

In setM method do this in first line :

this.m[i] = new MsgBean();

Problem is that you have instantiated your array but the objects are null inside that array. So you need to first instantiate the object at required index and then use it.

UPDATE FOR COMMENT

Make a getter method for your m like this getM() and annotate it with XmlElement(Remove the annotation from your current overloaded getM(int i) method). The method getM(int i) cannot be used by JAXB, since jaxb needs simple getter and setter for your properties.

Upvotes: 1

Related Questions