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):
------------------------------------------------------------------------------------------------------------------- - 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);
-
-
}
-
}
---------------------------------------------------------------------------------------------------------------
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
7 1460
First of all, use [ code ] brackets [ /code ] (without the spaces) to show code the way you want (and to show line numbers), like this: - 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, 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: - 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 nullPointerException, it would be arrayIndexOutOfBounds.
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
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 -
-
y[i+1].solutie=SolutiiDeTe.AdunareVectori(dy,y[i].solutie); //the problem
-
Oh and please use code tags if you have to post code.
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): - 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: - 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
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 - System.out.println(x[i].timp+" "+x[i].solutie);
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 - 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: -
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
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 - for (int i=0;i<nr_pasi+1;i++)
and not as did with - for (int i=0;i<nr_pasi;i++)
in the first loop and - for (int i=0;i<nr_pasi-1;i++)
three times thank you;i own you
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: gabryh |
last post by:
Hi,
The following code throws me NullPointerException.
.....
public static boolean isEmpty(String value) {
return ((value == null) || (value.trim().equals("")));
}
|
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...
|
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...
|
by: oll3i |
last post by:
private List<Klient> klienci;
m_add_client.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
|
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...
|
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...
|
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...
|
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...
|
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
{
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |