Results 1 to 14 of 14
  1. #1
    maestroX3 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Null Pointer Exception

    hey i created 2 classes, one containing all the methods (GraphData Class) and another to check whether the methods are working or not(Test Class). But i am getting Null pointer Exception. It would be great if anyone can help me out as i am new to java and this forum as well, thanx


    GraphData:

    Java Code:
    import java.util.*;	
    public class GraphData {
    		
    	
    	static final int SCATTER_XY = 1;
    	static final int BAR_GRAPH = 2;
    	
    	int width, height,diff;
    	int sNum=0;
    	int i=0;
    	int n=0;
    	int graphType;
    	int numofp;
        String Gname,xLabel,yLabel;
    	Double ScaleXtick, ScaleYtick, tickval,scaleXauto,scaleYauto,sx2,sy2,sx1,sy1;
    	Double x[], y[],xr[],yr[];
    	Double scaletypex;
    	Double scaletypey;
    	Double xi[][],yi[][];
    	Series_XY sxy;
    	Series_XY getsxy;
    	ArrayList<Series_XY> al=new ArrayList<Series_XY>();
    	int seriesNumber;
    
    	//defines a graph
    		public GraphData(int graphType,Series_XY sxy, Double scaletypex, Double scaletypey,int seriesNumber)
    		{
    			this.seriesNumber=sNum;
    			this.graphType = graphType;
    			this.sxy=sxy;	
    			this.scaletypex=scaletypex;
    			this.scaletypey=scaletypey;
    			
    		}
    
    		//sets graph type
    		public void setGraphType(int graphType)
    		{	
    			this.graphType=graphType;
    		}	
    		
    		
    		//adds new series
    		public int addSeriesXY(Series_XY sxyin)
    		{	
    			
    			
    			al.add(sNum, sxyin);
    			sNum++;
    			return sNum;
    		}
    		
    		
    		//automatic scale for x axis
    		public double automaticScaleX(double x[])
    		{
    			scaleXauto = (double)(width - 2*diff)/maxValXSer(sNum);
    			return scaleXauto;	
    		}
    		
    		
    		//automatic scale for y axis
    		public double automaticScaleY(double y[])
    		{
    			scaleYauto = (double)(height - 2*diff)/maxValYSer(sNum);
    			return scaleYauto;
    		}
    		
    		//manual scale for x axis
    		public double setScaleX(double ScaleXtick)
    		{
    			this.ScaleXtick=ScaleXtick;
    			return ScaleXtick;
    		}
    		
    		
    		//manual scale for y axis
    		public double setScaleY(double ScaleYtick)
    		{
    			this.ScaleYtick=ScaleYtick;
    			return ScaleYtick;
    		}
    		
    		
    		//returns scale for x axis
    		public double getScaleX()
    		{
    			if (scaletypex==0||scaletypex<0)
    			return scaleXauto;
    			else
    			return ScaleXtick;
    		}
    		
    		//returns scale for y axis
    		public double getScaleY()
    		{
    			if (scaletypey==0||scaletypey<0)
    				return scaleYauto;
    				else
    				return ScaleYtick;
    		}
    		
    		
    		//return number of points in a series
    		public int getNumOfPoints(int seriesNum)
    		{
    			sxy=al.get(sNum);
    			numofp=sxy.n;
    			return numofp;
    		}
    		
    		//return series
    		public Series_XY getSeriesXY(int seriesNum)
    		{
    			seriesNum=sNum;
    			Series_XY newsxy;
    			newsxy=al.get(seriesNum);
    			return newsxy;
    			
    		}
    		
    		//returns globally maximum x
    		public double maxValXGlo()
    		{
    			double max=0;
    			int p=0,m=0;
    			for(int k=0;k<sNum;k++)
    			{
    				m=getNumOfPoints(k);
    				p+=m;
    			}
    			for(int j=0;j<p;j++)
    			{
    				if(x[j]>max)
    					max=x[j];
    			}
    			return max;
    		}
    		
    		//returns globally maximum y
    		public double maxValYGlo()
    		{
    			double max=0;
    			int p=0,m=0;
    			for(int k=0;k<sNum;k++)
    			{
    				m=getNumOfPoints(k);
    				p+=m;
    			}
    			for(int j=0;j<p;j++)
    			{
    				if(y[j]>max)
    					max=y[j];
    			}
    			return max;
    			}
    		
    		
    		//returns globally minimum x
    		public double minValXGlo()
    		{
    			double min;
    			int p=0,m=0;
    			for(int k=0;k<sNum;k++)
    			{
    				m=getNumOfPoints(k);
    				p+=m;
    			}
    			min=x[0];
    			for(int j=0;j<p;j++)
    			{
    				if(x[j]<min)
    					min=x[j];
    			}
    			return min;
    		}
    		
    		//returns globally minimum y
    		public double minValYGlo()
    		{
    			double min;
    			int p=0,m=0;
    			for(int k=0;k<sNum;k++)
    			{
    				m=getNumOfPoints(k);
    				p+=m;
    			}
    			min=y[0];
    			for(int j=0;j<p;j++)
    			{
    				if(y[j]<min)
    					min=y[j];
    			}
    			return min;
    		}
    		
    		
    		//returns series maximum x
    		
    		public double maxValXSer(int seriesNum)
    		{
    			seriesNum=sNum;
    			double max=0;
    			int p;
    			p=getNumOfPoints(seriesNum);
    			for(int j=0;j<p;j++)
    			{
    				if(xi[j][seriesNum]>max)
    					max=xi[j][seriesNum];
    			}
    			return max;
    		}
    		
    		//returns series maximum y
    		public double maxValYSer(int seriesNum)
    		{
    			seriesNum=sNum;
    			double max=0;
    			int p;
    			p=getNumOfPoints(seriesNum);
    			for(int j=0;j<p;j++)
    			{
    				if(yi[j][seriesNum]>max)
    					max=yi[j][seriesNum];
    			}
    			return max;
    		}
    		
    		
    		//returns series minimum x
    		
    		public double minValXSer(int seriesNum)
    		{
    			seriesNum=sNum;
    			double min;
    			int p;
    			p=getNumOfPoints(seriesNum);
    			min=xi[0][seriesNum];
    			for(int j=0;j<p;j++)
    			{
    				if(xi[j][seriesNum]<min)
    					min=xi[j][seriesNum];
    			}
    			return min;
    		}
    		
    		
    		//returns series minimum y
    		
    		public double minValYSer(int seriesNum)
    		{
    			seriesNum=sNum;
    			double min;
    			int p;
    			p=getNumOfPoints(seriesNum);
    			min=yi[0][seriesNum];
    			for(int j=0;j<p;j++)
    			{
    				if(yi[j][seriesNum]<min)
    				min=yi[j][seriesNum];
    			}
    			return min;
    		}
    		
    		//sets x label
    		public void setLabelX(String Labelx)
    		{	
    			xLabel="x-axis";
    			sx1=(double)width/2;
    			sy1=(double)height-diff/2;
    		}
    		
    		//sets y label
    		public void setLabelY(String Labely)
    		{
    			xLabel="x-axis";
    			sx2=(double)width/2;
    			sy2=(double)height-diff/2;
    
    		}
    		
    		//gets graph type
    		public String getGraphType(String GraphName)
    		{
    			return GraphName;
    		}
    		
    		//sets new origin
    		public void setOrigin(double x0, double y0)
    		{
    		}
    		
    		//computes smooth curve
    		public void computeSmoothCurve(int seriesNum)
    		{
    		}
    		/*public static void main(String[] args){
    			int n=getNumOfPoints();
    			System.out.println(n);
    		}*/
    	}
    class Series_XY
    {
    	int n;
    	double x[],y[];
    
    	
    		Series_XY(double x[],double y[],int n)
    		{
    			this.x=x;
    			this.y=y;
    			this.n=n;
    		}
    		
    
    	}





    Test:

    Java Code:
    class Test
    {
    
    	public static double[] x={0,1,2,3,4};
    	public static double[] y={0,1,2,3,4};
    	
    	static Series_XY sxy;
    				
    	static int n=5;
    	static int gt=1;
    	static double scalex=5;
    	static double scaley=10;
    	static int nop;
    	static int sNum;
    	static int np,sn;
    			
    	
    		
    
    
    	
    public static void input(GraphData g)
    	{
    	Series_XY sxy=null;
    	for(int i=0;i<n;i++)
    		{
    		g.sxy.x[i]=x[i];
    		g.sxy.y[i]=y[i];
    		}
    	g.sxy.n=n;
    		
    	sNum=g.addSeriesXY(sxy);
    	g.seriesNumber=sNum;
    	
    		g.scaletypex=scalex;
    		g.scaletypey=scaley;
    		
    		//g.seriesNumber=g.addSeriesXY(g.sxy);
    		
    		g.graphType=gt;
    		g.setGraphType(g.graphType);
    		
    		
    		nop=g.getNumOfPoints(sNum);
    			
    	}	
    
    public static void main(String[] args)
    {
    	
    	GraphData g=new GraphData(gt, sxy, scalex, scalex, sNum);
    	input(g);
    	sn=g.sNum;
    	np=g.sxy.n;
    	System.out.println(sn);
    	System.out.println(np);
    }
    	
    	
    }
    Last edited by Fubarable; 05-22-2012 at 06:33 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null Pointer Exception

    Moderator edit: [code] [/code] tags added.
    Forum member comment: That's a heck of a lot of code to ask us to go through.

    It would help *greatly* if you indicated what line causes the NPE to occur.

  3. #3
    maestroX3 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Exception

    Sorry for the long code, NPE comes in lines 27 and 52 of Test class.
    PS: in the GraphData class the methods which will be useful right now for this thread are getNumofPoints, addSeriesXY,GraphData constructor and the SeriesXY class at the end, extremely sorry for the long code.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null Pointer Exception

    Quote Originally Posted by maestroX3 View Post
    Sorry for the long code, NPE comes in lines 27 and 52 of Test class.
    PS: in the GraphData class the methods which will be useful right now for this thread are getNumofPoints, addSeriesXY,GraphData constructor and the SeriesXY class at the end, extremely sorry for the long code.
    OK, so line 27 is:

    g.sxy.x[i]=x[i];

    and what object on this line is null? One way to find out is to do...

    Java Code:
    for(int i=0;i<n;i++)
    {
       System.out.println("g is null: " + (g == null));
       System.out.println("g.sxy is null: " + (g.sxy == null));
       System.out.println("g.sxy.x is null: " + (g.sxy.x == null));
       g.sxy.x[i]=x[i];
       g.sxy.y[i]=y[i];
    }
    and let us know what happens.

  5. #5
    maestroX3 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Exception

    It returns
    "g is null: false
    g.sxy is null: true"
    followed by NPE statement

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null Pointer Exception

    OK, so you know that the g object is OK, but its sxy field is null. Now trace back in your code to see why.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null Pointer Exception

    By trace back, I mean:

    1. The input method get's GraphicData, g, via a parameter.
    2. input is called in the main method.
    3. the GraphicData variable is declared and then initialized with an object in the main method, just before calling input(g).
    4. GraphicData is passed a static sxy parameter in its constructor.
    5. That sxy parameter is declared as a static variable of the Test class.
    6. Is that sxy variable ever initialized?

  8. #8
    maestroX3 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Exception

    am not getting where to initialize it, in GraphData, or in Input method, or before the Input method

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null Pointer Exception

    Quote Originally Posted by maestroX3 View Post
    am not getting where to initialize it, in GraphData, or in Input method, or before the Input method
    What does sxy represent logically?

  10. #10
    maestroX3 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Exception

    i am creating a graph basically, and all these are the methods that will be utilized in making it, and sxy there, is an object of type Series_XY which contains the x and y coordinates and the number of points entered!

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null Pointer Exception

    Quote Originally Posted by maestroX3 View Post
    i am creating a graph basically, and all these are the methods that will be utilized in making it, and sxy there, is an object of type Series_XY which contains the x and y coordinates and the number of points entered!
    OK, so you wouldn't initialize sxy in GraphData class, the class that displays the points, but rather in the class that creates the data points. So if you are going to test the GraphData class, then create your data points in the Test class, then pass them into the GraphData constructor.

  12. #12
    maestroX3 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Exception

    how do i initialize sxy, like it has 3 components x[], y[], n, so do i initialize each of them indivisually? Can you give an example of that?
    Last edited by maestroX3; 05-22-2012 at 08:43 PM.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Null Pointer Exception

    I really don't know, and I suppose I could figure it out if I had the time to study the code a bit, but I'm honestly confused -- this is your program right? So you should know a lot more about than any of us should. Or did you borrow this program from the web?

  14. #14
    maestroX3 is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Null Pointer Exception

    No its mine, its just that i haven't done any java before, i suddenly had to make this project, i took some help from my friends, and did the rest by myself and a li'l bit help from some forums. so my basics are not so good! that's why i get stuck up in small problems. thanx anyways :)

Similar Threads

  1. null pointer exception
    By Herah in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 08:44 AM
  2. null pointer exception
    By bequick01 in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 08:31 PM
  3. Null Pointer Exception HELP!?
    By 2wyked in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 01:41 AM
  4. Null pointer exception
    By Domo230 in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 10:21 AM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •