473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

NullPointerException

5
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):
-------------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4.  
  5.  
  6. class SolutiiDeTe{
  7.  
  8. double timp;
  9. double[] solutie;
  10.  
  11. public static double[] inmultireVectorCuScalar(double a, double[] b){
  12.  
  13.     double[] x=new double[b.length];
  14.     for(int i=0;i<b.length;i++)
  15.         x[i]=b[i]*a;
  16.     return x;
  17. }
  18.  
  19. public static double[] AdunareVectori(double[] a, double[] b){
  20.  
  21.     double[] x=new double[b.length];
  22.     for(int i=0;i<b.length;i++)
  23.         x[i]=b[i]+a[i];
  24.     return x;
  25. }    
  26.  
  27.  
  28.  
  29. public static double[] definireSIS(double[] y,double xi,double alfa,double beta){
  30.  
  31.     xi=(double)0;
  32.     double[] dy=new double[2];
  33.     dy[0]=-beta*y[0]*y[1]+alfa*y[1];
  34.     dy[1]=beta*y[0]*y[1]-alfa*y[1];
  35.     return dy;
  36. }
  37.  
  38. //public double[] definireSIRS(double[] y,double alfa,double beta){
  39. //    double[] dy=new double[3];
  40. //    dy[0]=-beta*y[0]*y[1];
  41. //    dy[1]=beta*y[0]*y[1]-alfa*y[1];
  42. //    dy[2]=alfa*y[1];
  43. //    return dy;
  44. //}
  45.  
  46. public static SolutiiDeTe[] RK_SIS(int an1,int an2,int N,double pas,double a,double b,int inf) {
  47.  
  48. double[] k1,k2,k3,k4,k;
  49.  
  50. int nr_pasi=(int)((an2-an1)/pas);
  51.  
  52. SolutiiDeTe[] y=new SolutiiDeTe[nr_pasi+1];
  53.  
  54. for (int i=0;i<nr_pasi;i++)
  55.     {y[i]=new SolutiiDeTe();
  56.     y[i].solutie=new double[2];
  57.     }
  58.  
  59. double[] dy=new double[nr_pasi+1];
  60.  
  61.  
  62. y[0].timp=an1;
  63. y[0].solutie[0]=(double)(N-inf);
  64. y[0].solutie[1]=(double)inf;
  65.  
  66. for (int i=0;i<nr_pasi;i++){
  67.     k1=new double[2];
  68.     k2=new double[2];
  69.     k3=new double[2];
  70.     k4=new double[2];
  71.     k=new double[2];
  72.     k1=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(y[i].solutie,y[i].timp,a,b));
  73.  
  74.     k=SolutiiDeTe.inmultireVectorCuScalar(0.5,k1);
  75.     k=SolutiiDeTe.AdunareVectori(y[i].solutie,k);
  76.  
  77.     k2=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+0.5*pas,a,b));
  78.  
  79.     k=SolutiiDeTe.inmultireVectorCuScalar(0.5,k2);
  80.     k=SolutiiDeTe.AdunareVectori(y[i].solutie,k);
  81.     k3=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+0.5*pas,a,b));
  82.     k=SolutiiDeTe.AdunareVectori(y[i].solutie,k3);
  83.     k4=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+pas,a,b));
  84.     k=SolutiiDeTe.inmultireVectorCuScalar(2,k2);
  85.     dy=SolutiiDeTe.AdunareVectori(k1,k);
  86.     k=SolutiiDeTe.inmultireVectorCuScalar(2,k2);    
  87.     dy=SolutiiDeTe.AdunareVectori(dy,k);
  88.     dy=SolutiiDeTe.AdunareVectori(dy,k4);
  89.     dy=SolutiiDeTe.inmultireVectorCuScalar(1/6,dy);
  90.  
  91.  
  92.     y[i+1].solutie=SolutiiDeTe.AdunareVectori(dy,y[i].solutie);   //line 92
  93.  
  94.     y[i+1].timp=((double)(Math.round((y[0].timp+pas*(i+1))*10)))/10;
  95. }
  96.  
  97. return y;}
  98.  
  99. public static void main(String[] args) {
  100.  
  101. SolutiiDeTe[] x=new SolutiiDeTe[21];
  102.  
  103. for (int i=0;i<21;i++)
  104.  
  105.     {x[i]=new SolutiiDeTe();
  106.      x[i].solutie=new double[2];     
  107.     }
  108.  
  109.  
  110.  
  111. x=SolutiiDeTe.RK_SIS(1,3,50,0.1,0.2,0.1,2);  //line 111
  112. for (int i=0;i<21;i++)
  113.     System.out.println(x[i].timp+" "+x[i].solutie);
  114.  
  115. }
  116. }
---------------------------------------------------------------------------------------------------------------

the exact error i get is

Exception in thread "main" java.lang.NullPointerException
at SolutiiDeTe.RK_SIS(SolutiiDeTe.java:92)
at SolutiiDeTe.main(SolutiiDeTe.java: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
May 24 '07 #1
7 1460
blazedaces
284 100+
First of all, use [ code ] brackets [ /code ] (without the spaces) to show code the way you want (and to show line numbers), like this:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4.  
  5.  
  6. class SolutiiDeTe{
  7.  
  8. double timp;
  9. double[] solutie;
  10.  
  11. public static double[] inmultireVectorCuScalar(double a, double[] b){
  12.  
  13.     double[] x=new double[b.length];
  14.     for(int i=0;i<b.length;i++)
  15.         x[i]=b[i]*a;
  16.     return x;
  17. }
  18.  
  19. public static double[] AdunareVectori(double[] a, double[] b){
  20.  
  21.     double[] x=new double[b.length];
  22.     for(int i=0;i<b.length;i++)
  23.         x[i]=b[i]+a[i];
  24.     return x;
  25. }    
  26.  
  27.  
  28.  
  29. public static double[] definireSIS(double[] y,double xi,double alfa,double beta){
  30.  
  31.     xi=(double)0;
  32.     double[] dy=new double[2];
  33.     dy[0]=-beta*y[0]*y[1]+alfa*y[1];
  34.     dy[1]=beta*y[0]*y[1]-alfa*y[1];
  35.     return dy;
  36. }
  37.  
  38. //public double[] definireSIRS(double[] y,double alfa,double beta){
  39. //    double[] dy=new double[3];
  40. //    dy[0]=-beta*y[0]*y[1];
  41. //    dy[1]=beta*y[0]*y[1]-alfa*y[1];
  42. //    dy[2]=alfa*y[1];
  43. //    return dy;
  44. //}
  45.  
  46. public static SolutiiDeTe[] RK_SIS(int an1,int an2,int N,double pas,double a,double b,int inf) {
  47.  
  48. double[] k1,k2,k3,k4,k;
  49.  
  50. int nr_pasi=(int)((an2-an1)/pas);
  51.  
  52. SolutiiDeTe[] y=new SolutiiDeTe[nr_pasi+1];
  53.  
  54. for (int i=0;i<nr_pasi;i++)
  55.     {y[i]=new SolutiiDeTe();
  56.     y[i].solutie=new double[2];
  57.     }
  58.  
  59. double[] dy=new double[nr_pasi+1];
  60.  
  61.  
  62. y[0].timp=an1;
  63. y[0].solutie[0]=(double)(N-inf);
  64. y[0].solutie[1]=(double)inf;
  65.  
  66. for (int i=0;i<nr_pasi;i++){
  67.     k1=new double[2];
  68.     k2=new double[2];
  69.     k3=new double[2];
  70.     k4=new double[2];
  71.     k=new double[2];
  72.     k1=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(y[i].solutie,y[i].timp,a,b));
  73.  
  74.     k=SolutiiDeTe.inmultireVectorCuScalar(0.5,k1);
  75.     k=SolutiiDeTe.AdunareVectori(y[i].solutie,k);
  76.  
  77.     k2=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+0.5*pas,a,b));
  78.  
  79.     k=SolutiiDeTe.inmultireVectorCuScalar(0.5,k2);
  80.     k=SolutiiDeTe.AdunareVectori(y[i].solutie,k);
  81.     k3=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+0.5*pas,a,b));
  82.     k=SolutiiDeTe.AdunareVectori(y[i].solutie,k3);
  83.     k4=SolutiiDeTe.inmultireVectorCuScalar(pas,SolutiiDeTe.definireSIS(k,y[i].timp+pas,a,b));
  84.     k=SolutiiDeTe.inmultireVectorCuScalar(2,k2);
  85.     dy=SolutiiDeTe.AdunareVectori(k1,k);
  86.     k=SolutiiDeTe.inmultireVectorCuScalar(2,k2);    
  87.     dy=SolutiiDeTe.AdunareVectori(dy,k);
  88.     dy=SolutiiDeTe.AdunareVectori(dy,k4);
  89.     dy=SolutiiDeTe.inmultireVectorCuScalar(1/6,dy);
  90.  
  91.  
  92.     y[i+1].solutie=SolutiiDeTe.AdunareVectori(dy,y[i].solutie);   //line 92
  93.  
  94.     y[i+1].timp=((double)(Math.round((y[0].timp+pas*(i+1))*10)))/10;
  95. }
  96.  
  97. return y;}
  98.  
  99. public static void main(String[] args) {
  100.  
  101. SolutiiDeTe[] x=new SolutiiDeTe[21];
  102.  
  103. for (int i=0;i<21;i++)
  104.  
  105.     {x[i]=new SolutiiDeTe();
  106.      x[i].solutie=new double[2];     
  107.     }
  108.  
  109.  
  110.  
  111. x=SolutiiDeTe.RK_SIS(1,3,50,0.1,0.2,0.1,2);  //line 111
  112. for (int i=0;i<21;i++)
  113.     System.out.println(x[i].timp+" "+x[i].solutie);
  114.  
  115. }
  116. }
Now, NullPointerException 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:
Expand|Select|Wrap|Line Numbers
  1. public static double[] AdunareVectori(double[] a, double[] b){
  2.  
  3.     double[] x=new double[b.length];
  4.     for(int i=0;i<b.length;i++)
  5.         x[i]=b[i]+a[i];
  6.     return x;
  7. }    
  8.  
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 nullPointerException, it would be arrayIndexOutOfBounds.
May 24 '07 #2
geenie
5
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=SolutiiDeTe.AdunareVectori(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
May 25 '07 #3
r035198x
13,262 8TB
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=SolutiiDeTe.AdunareVectori(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
Expand|Select|Wrap|Line Numbers
  1.  
  2. y[i+1].solutie=SolutiiDeTe.AdunareVectori(dy,y[i].solutie); //the problem 
  3.  
Oh and please use code tags if you have to post code.
May 25 '07 #4
blazedaces
284 100+
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):
Expand|Select|Wrap|Line Numbers
  1. SolutiiDeTe[] y=new SolutiiDeTe[nr_pasi+1];
  2.  
  3. for (int i=0;i<nr_pasi;i++)
  4.     {y[i]=new SolutiiDeTe();
  5.     y[i].solutie=new double[2];
  6.     }
  7.  
  8. // Not important lines...
  9.  
  10. for (int i=0;i<nr_pasi;i++){
  11.     //Not important
  12.  
  13.     y[i+1].solutie=SolutiiDeTe.AdunareVectori(dy,y[i].solutie);   //line 92
  14.  
  15.     y[i+1].timp=((double)(Math.round((y[0].timp+pas*(i+1))*10)))/10;
  16. }
  17.  
  18.  
  19. }
  20. }
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:
Expand|Select|Wrap|Line Numbers
  1. 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
May 25 '07 #5
geenie
5
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" NullPointerException
at SolutiiDeTe.main(SolutiiDeTe.java:115)
meaning here

Expand|Select|Wrap|Line Numbers
  1.     System.out.println(x[i].timp+" "+x[i].solutie);
May 25 '07 #6
blazedaces
284 100+
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" NullPointerException
at SolutiiDeTe.main(SolutiiDeTe.java:115)
meaning here

Expand|Select|Wrap|Line Numbers
  1.     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:
Expand|Select|Wrap|Line Numbers
  1. for (int i=0;i<21;i++)
  2.         for(int j = 0; j < 2; j++) {
  3.             System.out.println(x[i].timp+" "+x[i].solutie[j]);
  4.         }
  5.  
  6. }
  7.  
Tell me if this helped or if I was off-track,
-blazed
May 25 '07 #7
geenie
5
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 NullPointerException,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
Expand|Select|Wrap|Line Numbers
  1. for (int i=0;i<nr_pasi+1;i++)
and not as did with
Expand|Select|Wrap|Line Numbers
  1. for (int i=0;i<nr_pasi;i++)
in the first loop and
Expand|Select|Wrap|Line Numbers
  1. for (int i=0;i<nr_pasi-1;i++)
three times thank you;i own you
May 25 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: K S Aldebaraan | last post by:
I'm trying to submit a form with an action of a servlet, and a view equal to the same jsp page. I'm not sure what I'm doing wrong, but keep getting a NullPointerException on the second line of...
4
by: gabryh | last post by:
Hi, The following code throws me NullPointerException. ..... public static boolean isEmpty(String value) { return ((value == null) || (value.trim().equals(""))); }
0
by: Old-timer | last post by:
Not sure where else to post this. I'm sure I'm doing something wrong, but I wouldn't think a simple app would give me so much trouble. I've got a small test java class that I'm trying to have...
2
by: Smith | last post by:
The program compiled successfully, but it gives the following error on runtime.... java.lang.NullPointerException at FrogManiaApp.paint(FrogManiaApp.java:102) at...
13
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ...
1
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private...
2
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
3
by: chris123456789 | last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs: import java.util.*; import java.io.*; public class Decrypt { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.