NullPointerException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geenie
    New Member
    • May 2007
    • 5

    #1

    NullPointerException

    i`m developind my second program using java which is supposed to solve a system of ordinary differential equations(S'=-beta*S*I-alfa*I;I'=beta* S*I+alfa*I) using runge kutta method;my first program was supposde to solve a single eq and worked just great(after a initial nullpointer exception error ,which reading some things on the internet i managed to eliminate).now i get the same error and i figure it has something to do with dereferencing ,but no matter how hard i tried i haven`t solved the problem;
    the code source looks like that(actually it`s now the final implementation of my algorithm,i`m just trying to test some things):
    -------------------------------------------------------------------------------------------------------------------
    [code=java]import java.io.*;
    import java.util.Array List;



    class SolutiiDeTe{

    double timp;
    double[] solutie;

    public static double[] inmultireVector CuScalar(double a, double[] b){

    double[] x=new double[b.length];
    for(int i=0;i<b.length; i++)
    x[i]=b[i]*a;
    return x;
    }

    public static double[] AdunareVectori( double[] a, double[] b){

    double[] x=new double[b.length];
    for(int i=0;i<b.length; i++)
    x[i]=b[i]+a[i];
    return x;
    }



    public static double[] definireSIS(dou ble[] y,double xi,double alfa,double beta){

    xi=(double)0;
    double[] dy=new double[2];
    dy[0]=-beta*y[0]*y[1]+alfa*y[1];
    dy[1]=beta*y[0]*y[1]-alfa*y[1];
    return dy;
    }

    //public double[] definireSIRS(do uble[] y,double alfa,double beta){
    // double[] dy=new double[3];
    // dy[0]=-beta*y[0]*y[1];
    // dy[1]=beta*y[0]*y[1]-alfa*y[1];
    // dy[2]=alfa*y[1];
    // return dy;
    //}

    public static SolutiiDeTe[] RK_SIS(int an1,int an2,int N,double pas,double a,double b,int inf) {

    double[] k1,k2,k3,k4,k;

    int nr_pasi=(int)(( an2-an1)/pas);

    SolutiiDeTe[] y=new SolutiiDeTe[nr_pasi+1];

    for (int i=0;i<nr_pasi;i ++)
    {y[i]=new SolutiiDeTe();
    y[i].solutie=new double[2];
    }

    double[] dy=new double[nr_pasi+1];


    y[0].timp=an1;
    y[0].solutie[0]=(double)(N-inf);
    y[0].solutie[1]=(double)inf;

    for (int i=0;i<nr_pasi;i ++){
    k1=new double[2];
    k2=new double[2];
    k3=new double[2];
    k4=new double[2];
    k=new double[2];
    k1=SolutiiDeTe. inmultireVector CuScalar(pas,So lutiiDeTe.defin ireSIS(y[i].solutie,y[i].timp,a,b));

    k=SolutiiDeTe.i nmultireVectorC uScalar(0.5,k1) ;
    k=SolutiiDeTe.A dunareVectori(y[i].solutie,k);

    k2=SolutiiDeTe. inmultireVector CuScalar(pas,So lutiiDeTe.defin ireSIS(k,y[i].timp+0.5*pas,a ,b));

    k=SolutiiDeTe.i nmultireVectorC uScalar(0.5,k2) ;
    k=SolutiiDeTe.A dunareVectori(y[i].solutie,k);
    k3=SolutiiDeTe. inmultireVector CuScalar(pas,So lutiiDeTe.defin ireSIS(k,y[i].timp+0.5*pas,a ,b));
    k=SolutiiDeTe.A dunareVectori(y[i].solutie,k3);
    k4=SolutiiDeTe. inmultireVector CuScalar(pas,So lutiiDeTe.defin ireSIS(k,y[i].timp+pas,a,b)) ;
    k=SolutiiDeTe.i nmultireVectorC uScalar(2,k2);
    dy=SolutiiDeTe. AdunareVectori( k1,k);
    k=SolutiiDeTe.i nmultireVectorC uScalar(2,k2);
    dy=SolutiiDeTe. AdunareVectori( dy,k);
    dy=SolutiiDeTe. AdunareVectori( dy,k4);
    dy=SolutiiDeTe. inmultireVector CuScalar(1/6,dy);


    y[i+1].solutie=Soluti iDeTe.AdunareVe ctori(dy,y[i].solutie); //line 92

    y[i+1].timp=((double) (Math.round((y[0].timp+pas*(i+1) )*10)))/10;
    }

    return y;}

    public static void main(String[] args) {

    SolutiiDeTe[] x=new SolutiiDeTe[21];

    for (int i=0;i<21;i++)

    {x[i]=new SolutiiDeTe();
    x[i].solutie=new double[2];
    }



    x=SolutiiDeTe.R K_SIS(1,3,50,0. 1,0.2,0.1,2); //line 111
    for (int i=0;i<21;i++)
    System.out.prin tln(x[i].timp+" "+x[i].solutie);

    }
    }[/code]
    ---------------------------------------------------------------------------------------------------------------

    the exact error i get is

    Exception in thread "main" java.lang.NullP ointerException
    at SolutiiDeTe.RK_ SIS(SolutiiDeTe .java:92)
    at SolutiiDeTe.mai n(SolutiiDeTe.j ava:111)
    ---------------------------------------------------------------------------------------------------------------
    i applologise for how my code looks like(i copied from jcreator but i doesn`t look in here how i expected to);i`ve marked marked the lines 92 and 111 for you
    i would extremely apreciate any help;looking forward for reply;
    thank you
    Last edited by JosAH; May 24 '07, 09:02 PM. Reason: added [code=java] ... [/code] tags
  • blazedaces
    Contributor
    • May 2007
    • 284

    #2
    First of all, use [ code ] brackets [ /code ] (without the spaces) to show code the way you want (and to show line numbers), like this:
    Code:
    import java.io.*;
    import java.util.ArrayList;
    
    
    
    class SolutiiDeTe{
    
    double timp;
    double[] solutie;
    
    public static double[] inmultireVectorCuScalar(double a, double[] b){
    
    	double[] x=new double[b.length];
    	for(int i=0;i<b.length;i++)
    		x[i]=b[i]*a;
    	return x;
    }
    
    public static double[] AdunareVectori(double[] a, double[] b){
    
    	double[] x=new double[b.length];
    	for(int i=0;i<b.length;i++)
    		x[i]=b[i]+a[i];
    	return x;
    }	
    
     
    
    public static double[] definireSIS(double[] y,double xi,double alfa,double beta){
    	
    	xi=(double)0;
    	double[] dy=new double[2];
    	dy[0]=-beta*y[0]*y[1]+alfa*y[1];
    	dy[1]=beta*y[0]*y[1]-alfa*y[1];
    	return dy;
    }
    
    //public double[] definireSIRS(double[] y,double alfa,double beta){
    //	double[] dy=new double[3];
    //	dy[0]=-beta*y[0]*y[1];
    //	dy[1]=beta*y[0]*y[1]-alfa*y[1];
    //	dy[2]=alfa*y[1];
    //	return dy;
    //}
    
    public static SolutiiDeTe[] RK_SIS(int an1,int an2,int N,double pas,double a,double b,int inf) {
    
    double[] k1,k2,k3,k4,k;
    
    int nr_pasi=(int)((an2-an1)/pas);
    
    SolutiiDeTe[] y=new SolutiiDeTe[nr_pasi+1];
    
    for (int i=0;i<nr_pasi;i++)
    	{y[i]=new SolutiiDeTe();
    	y[i].solutie=new double[2];
    	}
    	 
    double[] dy=new double[nr_pasi+1];
    
    
    y[0].timp=an1;
    y[0].solutie[0]=(double)(N-inf);
    y[0].solutie[1]=(double)inf;
    
    for (int i=0;i<nr_pasi;i++){
    	k1=new double[2];
    	k2=new double[2];
    	k3=new double[2];
    	k4=new double[2];
    	k=new double[2];
    	k1=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(y[i].solutie,y[i].timp,a,b));
    
    	k=SolutiiDeTe.inmultireVectorCuScalar(0.5,k1);
    	k=SolutiiDeTe.AdunareVectori(y[i].solutie,k);
    
    	k2=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+0.5*pas,a,b));
    
    	k=SolutiiDeTe.inmultireVectorCuScalar(0.5,k2);
    	k=SolutiiDeTe.AdunareVectori(y[i].solutie,k);
    	k3=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+0.5*pas,a,b));
    	k=SolutiiDeTe.AdunareVectori(y[i].solutie,k3);
    	k4=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+pas,a,b));
    	k=SolutiiDeTe.inmultireVectorCuScalar(2,k2);
    	dy=SolutiiDeTe.AdunareVectori(k1,k);
    	k=SolutiiDeTe.inmultireVectorCuScalar(2,k2);	
    	dy=SolutiiDeTe.AdunareVectori(dy,k);
    	dy=SolutiiDeTe.AdunareVectori(dy,k4);
    	dy=SolutiiDeTe.inmultireVectorCuScalar(1/6,dy);
    	
    	
    	y[i+1].solutie=SolutiiDeTe.AdunareVectori(dy,y[i].solutie);   //line 92
    
    	y[i+1].timp=((double)(Math.round((y[0].timp+pas*(i+1))*10)))/10;
    }
    	
    return y;}
    
    public static void main(String[] args) {
    
    SolutiiDeTe[] x=new SolutiiDeTe[21];
    
    for (int i=0;i<21;i++)
    
    	{x[i]=new SolutiiDeTe();
    	 x[i].solutie=new double[2];	 
    	}
    	
    
    	
    x=SolutiiDeTe.RK_SIS(1,3,50,0.1,0.2,0.1,2);  //line 111
    for (int i=0;i<21;i++)
    	System.out.println(x[i].timp+" "+x[i].solutie);
    
    }
    }
    Now, NullPointerExce ption happens basically when you try to do something on an object that does exist at that point in time. For example, Object objectname; (the object still doesn't exist) until you do objectname = new Object();

    I remember also in C++ whenever I divided by 0 it would give me a similar problem. Did this information help?

    -blazed

    P.S.

    Ok, this might be something:

    take a look at the method called at line 92:
    Code:
    public static double[] AdunareVectori(double[] a, double[] b){
    
    	double[] x=new double[b.length];
    	for(int i=0;i<b.length;i++)
    		x[i]=b[i]+a[i];
    	return x;
    }
    If a[i] has fewer elements then b[i] (you should test for this in the code anyway) then perhaps at some point a[i] does not exist anymore. Although, I don't believe that would be a nullPointerExce ption, it would be arrayIndexOutOf Bounds.

    Comment

    • geenie
      New Member
      • May 2007
      • 5

      #3
      i am sure it`s not about the fact that those 2 arrays would have different lenghts; and,yeah i`ve read a thousand times that an object doesn`t exist unless you make exist by using " new something" but i still don`t understand what i should in my case;what i would like to know is wheather the error has to something with this:

      ------------------------------------------------------------------------------------------------------------------
      class SolutiiDeTe{

      double timp;
      double[] solutie;

      //code lines that don`t make my interest right now//

      public static SolutiiDeTe[] RK_SIS(int an1,int an2,int N,double pas,double a,double b,int inf) {

      //other lines//

      SolutiiDeTe[] y=new SolutiiDeTe[nr_pasi+1];

      for (int i=0;i<nr_pasi;i ++)
      {y[i]=new SolutiiDeTe();
      y[i].solutie=new double[2];
      }
      //other lines

      y[i+1].solutie=Soluti iDeTe.AdunareVe ctori(dy,y[i].solutie); //the problem line

      ---------------------------------------------------------------------------------------------------------------
      doesn`t y[i].solutie exist?
      and how exactly should i call the j-th element of the y[i].solutie array ;all the ways i`ve tried give errors;
      sorry for bothering you too much

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by geenie
        i am sure it`s not about the fact that those 2 arrays would have different lenghts; and,yeah i`ve read a thousand times that an object doesn`t exist unless you make exist by using " new something" but i still don`t understand what i should in my case;what i would like to know is wheather the error has to something with this:

        ------------------------------------------------------------------------------------------------------------------
        class SolutiiDeTe{

        double timp;
        double[] solutie;

        //code lines that don`t make my interest right now//

        public static SolutiiDeTe[] RK_SIS(int an1,int an2,int N,double pas,double a,double b,int inf) {

        //other lines//

        SolutiiDeTe[] y=new SolutiiDeTe[nr_pasi+1];

        for (int i=0;i<nr_pasi;i ++)
        {y[i]=new SolutiiDeTe();
        y[i].solutie=new double[2];
        }
        //other lines

        y[i+1].solutie=Soluti iDeTe.AdunareVe ctori(dy,y[i].solutie); //the problem line

        ---------------------------------------------------------------------------------------------------------------
        doesn`t y[i].solutie exist?
        and how exactly should i call the j-th element of the y[i].solutie array ;all the ways i`ve tried give errors;
        sorry for bothering you too much
        which i are you refering to outside the for in
        [CODE=java]
        y[i+1].solutie=Soluti iDeTe.AdunareVe ctori(dy,y[i].solutie); //the problem
        [/CODE]

        Oh and please use code tags if you have to post code.

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          I think I may have found your problem!!! It's exactly what you were complaining about, you missed one "= new SolutiiDete();"

          See, look at just these two for loops down here (again, use code brackets next time):
          Code:
          SolutiiDeTe[] y=new SolutiiDeTe[nr_pasi+1];
          
          for (int i=0;i<nr_pasi;i++)
          	{y[i]=new SolutiiDeTe();
          	y[i].solutie=new double[2];
          	}
          
          // Not important lines...
          
          for (int i=0;i<nr_pasi;i++){
          	//Not important
          
          	y[i+1].solutie=SolutiiDeTe.AdunareVectori(dy,y[i].solutie);   //line 92
          
          	y[i+1].timp=((double)(Math.round((y[0].timp+pas*(i+1))*10)))/10;
          }
          	
          
          }
          }
          See, both your for loops are i < nr_pasi but when you declare all the "new new SolutiiDete();" stuff it's all y[i]. Yet, in the next for loop you do this:
          Code:
          y[i+1].solutie=SolutiiDeTe.AdunareVectori(dy,y[i].solutie);
          So you get to y[i+1]. Try changing your first for loop to i<nr_pasi+1 and see if that fixes the problem. I'm pretty sure it should...

          Good luck,
          -blazed

          Comment

          • geenie
            New Member
            • May 2007
            • 5

            #6
            blazedaces ,you were quite right; i in the second loop should have values between 0 and (nr_pasi-1);now i got another problem,which i suppose has to do exactly with what i have asked in my second post :about the j-th element of the y[i].solutie array;
            what i get is:
            Error in thread "main" NullPointerExce ption
            at SolutiiDeTe.mai n(SolutiiDeTe.j ava:115)
            meaning here

            Code:
            	System.out.println(x[i].timp+" "+x[i].solutie);

            Comment

            • blazedaces
              Contributor
              • May 2007
              • 284

              #7
              Originally posted by geenie
              blazedaces ,you were quite right; i in the second loop should have values between 0 and (nr_pasi-1);now i got another problem,which i suppose has to do exactly with what i have asked in my second post :about the j-th element of the y[i].solutie array;
              what i get is:
              Error in thread "main" NullPointerExce ption
              at SolutiiDeTe.mai n(SolutiiDeTe.j ava:115)
              meaning here

              Code:
              	System.out.println(x[i].timp+" "+x[i].solutie);
              I may not be understanding what the problem is, but if I'm right this is the solution to your problem:
              Code:
              for (int i=0;i<21;i++)
                      for(int j = 0; j < 2; j++) {
                      	System.out.println(x[i].timp+" "+x[i].solutie[j]);
                      }
              
              }
              Tell me if this helped or if I was off-track,
              -blazed

              Comment

              • geenie
                New Member
                • May 2007
                • 5

                #8
                during the time between my last post and yours j-th managed to call the j-th element of x[i].solutie using the "clone" method but i was still getting the NullPointerExce ption,that after printed me the (length(x[i].solutie)-1)th element and i figured it out;you were not quite right but absolutely right :in the first loop i should take values between 0 and nr_pasi(meaning i should have right
                Code:
                for (int i=0;i<nr_pasi+1;i++)
                and not as did with
                Code:
                for (int i=0;i<nr_pasi;i++)
                in the first loop and
                Code:
                for (int i=0;i<nr_pasi-1;i++)
                three times thank you;i own you

                Comment

                Working...