Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: NullPointException errors in code?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointException errors in code?

    Hello!
    I'm kind of super confused on this in general.

    In this code, I'm testing it through the main method that I put at the end of the code and essentially, I'm supposed to be testing that the methods execute correctly.

    I keep getting nullPointException errors which I essentially know what they are, but I'm having difficulty tracing it back to where exactly doesn't have any memory address path.

    Also, I didn't create a mode method completely yet.

    Here's my code:

    public class Stat {
     
     
    		private double [] data;
     
    		int x = 0;
    		public Stat()
    		{
    			//default constructor
    			double [] dataArray = new double [x];
    			dataArray[x] = 0.0;
    		}
     
     
    		public Stat(double[] d)
    		{
     
    			data = new double [d.length];
    			for (int i = 0; i<d.length; i++)
    			{
    				data[i] = d[i];
    			}
     
    		}
     
    		private double [] getDataArray;
    		public double [] getData()
    		{
    			getDataArray = new double [data.length];
     
    			for (int i = 0; i < data.length; i++)
    			{
    				data [i] = getDataArray[i];
    			}
     
    			return getDataArray;
    		}
     
    		private double [] setDataArray;
    		public void setData (double [] d) 
    		{
    			setDataArray = new double [d.length];
     
    			for (int i =0; i<d.length; i++)
    			{
    				d[i] = setDataArray[i];
    			}
     
    			data = setDataArray;
    		}
     
    		public boolean equals(Stat s)
    		{
    			if (data.length == s.getData().length)
    			{
    				for (int i = 0; i < s.getData().length; i++)
    				{
    					data[i] = s.getData()[i];
    				}
    				return true;
    			}
    			else
    			{
    				return false;
    			}
    		}
     
    		public String toString()
    		{
    			return "[" + data + "]";
     
    		}
     
    		double min = data[0];
    		public double min()
    		{
    			for (int i = 0; i < data.length; i++)
    			{
    				if (min > data[i])
    				{
    					min = data[i];
    				}
    			}
     
    			return min;
    		}
     
    		double max = data[0];
    		public double max()
    		{
    			for (int i = 0; i < data.length; i++)
    			{
    				if (max < data[i])
    				{
    					max = data[i];
    				}
    			}
     
    			return max;
    		}
     
    		public double average()
    		{
    			double sum = 0;
    			double numOfElements = 0;
    			for (int i = 0; i<data.length; i++)
    			{
    				sum = sum + data[i];
    				numOfElements = numOfElements + i;
    			}
     
    			return sum/numOfElements;
     
    		}
     
    		public double mode()
    		{
    			//go through each element in an array and compare it to the next element and see if it is the same, then add that one to count
     
    			for (int i =0; i<data.length; i++)
    			{
     
     
    			}
     
    			return 0.0;
     
    		}
     
     
     
     
     
    		public static void main(String[] args)
    		{
    			double[] data = {-5.3, 2.5, 88.9, 0, 0.0, 28, 16.5, 88.9, 109.5, -90, 88.9};
    			double[] data2 = {100.34, 50.01, 50.01, -8};
    			Stat stat1 = new Stat();
    			System.out.println("stat1 data = " + stat1.toString());
    			stat1 = new Stat(data);
    			System.out.println("stat1 has been altered.");
    			System.out.println("stat1 data = " + stat1.toString());
    			System.out.println("stat1 min = " + stat1.min());
    			System.out.println("stat1 max = " + stat1.max());
    			System.out.println("stat1 average = " + stat1.average());
    			System.out.println("stat1 mode = " + stat1.mode() + "\n");
    			Stat stat2 = new Stat();
    			stat2.setData(data2);
    			Stat stat3 = new Stat(stat1.getData());
    			System.out.println("stat2 data = " + stat2.toString());
    			System.out.println("stat3 data = " + stat3.toString());
    			System.out.println();
    			System.out.println("stat1 is equal to stat2 using \"equals()\"? " +
    			stat1.equals(stat2));
    			System.out.println("stat1 is equal to stat3 using \"equals()\"? " +
    			stat1.equals(stat3));
    			System.out.println("stat1 is equal to stat3 using \"==\"? " + (stat1== stat3));
    		}
     
    }

    Thank you so much!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointException errors in code?

    getting nullPointException errors
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointException errors in code?

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.
    I actually kind of figured it out! I'm sorry! Can I ask you about another problem though?
    Now when I print stuff out, it's also printing out memory addresses which I don't understand why.
    Also I believe there's something wrong with the getData method.
    Thank you so much for helping me sort this out!

    Here's the code:
    public class Stat {
     
     
    	private double [] data;
     
     
    	public Stat()
    	{
    		//default constructor
    		data = new double[1];
    		data[0] = 0.0;
    	}
     
     
    	public Stat(double[] d)
    	{
     
    		data = new double [d.length];
    		for (int i = 0; i<d.length; i++)
    		{
    			data[i] = d[i];
    		}
     
    	}
     
     
    	public double [] getData()
     
    	{
    		double[] temp = new double[data.length];
     
    		for (int i = 0; i < data.length; i++) 
    		{
    			data[i] = temp[i];
    		}
    		return temp;
    	}
     
    	public void setData (double [] d) 
    	{
     
    		data = new double[d.length];
     
            for (int i = 0; i < d.length; i++) 
            {
                    data[i] = d[i];
            }
    	}
     
    	public boolean equals(Stat s)
    	{
     
    		 double[] temp = s.getData();
     
             if (data.length != temp.length) 
             {
            	 return false;
             }
             for (int i = 0; i < data.length; i++)
             {
            	 if (data[i] != temp[i]) return false;
             }
             return true;
             //feel like the return true is in a wrong place?
     
     
    	}
     
    	public String toString()
    	{
    		System.out.println(data);
            String temp = "[";
     
            for (int i = 0; i < data.length; i++) {
                    temp = temp + data[i];
                    if (i < data.length - 1) 
                    {
                    	temp = temp + ", ";
                    }
            }
            return temp + "]";
            //will be a pair of braces
     
    	}
     
     
    	public double min()
    	{
     
    		if (data.length == 0) 
    		{
    			return 0.0;
    		}
     
            double min = data[0];
     
            for (int i = 1; i < data.length; i++) 
            {
            	if (min > data[i]) 
            	{
            		min = data[i];
            	}
            }
            return min;
     
    	}
     
     
    	public double max()
    	{
    		if (data.length == 0) 
    		{
    			return 0.0;
    		}
            double max = data[0];
     
            for (int i = 1; i < data.length; i++) 
            {
            	if (max < data[i])
            	{
            		max = data[i];
            	}
            }
            return max;
    	}
     
    	public double average()
    	{
     
    		if (data.length == 0) 
    		{
    			return 0.0;
    		}
     
            double sum = 0;
            for (int i = 0; i < data.length; i++)
            {
            	sum = sum + data[i];
            }
            return sum / data.length;
    	}
     
    	public double mode()
    	{
    		//go through each element in an array and compare it to the next element and see if it is the same, then add that one to count
     
    		for (int i =0; i<data.length; i++)
    		{
     
     
    		}
     
    		return 0.0;
     
    	}
     
     
     
     
     
    	public static void main(String[] args)
    	{
    		System.out.println("Example 1\n");
     
    		double[] data = {-5.3, 2.5, 88.9, 0, 0.0, 28, 16.5, 88.9, 109.5, -90, 88.9};
    		double[] data2 = {100.34, 50.01, 50.01, -8};
    		Stat stat1 = new Stat();
    		//is printing out the memory address when the above is executed
    		System.out.println("stat1 data = " + stat1.toString());
    		stat1 = new Stat(data);
    		System.out.println("stat1 has been altered.");
    		//is printing out the memory address 
    		System.out.println("stat1 data = " + stat1.toString());
    		System.out.println("stat1 min = " + stat1.min());
    		System.out.println("stat1 max = " + stat1.max());
    		System.out.println("stat1 average = " + stat1.average());
    		System.out.println("stat1 mode = " + stat1.mode() + "\n");
    		Stat stat2 = new Stat();
    		//printing out memory address
    		stat2.setData(data2);
    		Stat stat3 = new Stat(stat1.getData());
    		//probably something wrong with getData
    		System.out.println("stat2 data = " + stat2.toString());
    		//printing out memory address
    		System.out.println("stat3 data = " + stat3.toString());
    		//not printing out correctly, is just printing out the default value of the stat3
    		System.out.println();
    		System.out.println("stat1 is equal to stat2 using \"equals()\"? " +
    				stat1.equals(stat2));
    		System.out.println("stat1 is equal to stat3 using \"equals()\"? " +
    				stat1.equals(stat3));
    		System.out.println("stat1 is equal to stat3 using \"==\"? " + (stat1== stat3));
     
                   }
        }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointException errors in code?

    it's also printing out memory addresses
    Please post what is being printed and show what statement is printing it.

    It sounds like you are printing an object variable. The default toString() method returns the classname, an @ and a hex number. To control what is printed, add a toString() method to the class that returns the String that you want to see printed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointException errors in code?

    lol! I'm so sorry, I keep figuring stuff out after you give me these helpful hints!
    I was printing out data for some reason at the beginning of my toString method.

    So, I was given this "example 1" from my teacher to test this code.
    When I'm printing out "stat3" in the example, it just prints out 0.0 (b/c of the default constructor) the same number of times of the length of the array. But I'm trying to make it have the same elements in the same order as the data array. the example my teacher gave me uses the getData method to do this which makes me think the getData method isn't working.

    What is being printed out is :
    Example 1

    stat1 data = [0.0]
    stat1 has been altered.
    stat1 data = [-5.3, 2.5, 88.9, 0.0, 0.0, 28.0, 16.5, 88.9, 109.5, -90.0, 88.9]
    stat1 min = -90.0
    stat1 max = 109.5
    stat1 average = 29.80909090909091
    stat1 mode = 0.0

    stat2 data = [100.34, 50.01, 50.01, -8.0]
    stat3 data = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

    stat1 is equal to stat2 using "equals()"? false
    stat1 is equal to stat3 using "equals()"? true
    stat1 is equal to stat3 using "=="? false

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointException errors in code?

    Is the problem solved now?
    If not, please explain.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Someone help me with my code it has some errors? Thanks!
    By skitheeast8 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2012, 06:37 PM
  2. weird errors in my code
    By jobje325 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 17th, 2012, 11:15 AM
  3. Query gives NullPointException, JAVA EE
    By thomasdb in forum JDBC & Databases
    Replies: 0
    Last Post: August 16th, 2012, 05:28 PM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 06:10 AM