Results 1 to 6 of 6
  1. #1
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default compiles; no display

    Im creating an applet where you simply choose a planet from the combo box, hit enter and proceeds to display the output of miles from the array into the textArea.
    It compiles but when I run it, it doesnt do anything and then points me to the line of code below.

    Any ideas??

    public void getDistances()
    {
    millionsOfMiles = distancee.getDistances();
    displayData();
    }


    Java Code:
    /*	document segment
    	filename:	DistancePlanet
    	author:		Walker
    	date:		December.2010
    */
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class DistancePlanet extends JApplet implements ActionListener
    {
    /* ----------------------------- declarations */
    
    	// color objects
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    
    	Distancee distancee;
    
    	// components
    	JLabel planet;
    	JComboBox planetCombo;
    
    	JTextArea textA;
    
    	JButton enterJButton;
    	JButton clearJButton;
    
    	// variables
    	String[] planetArray = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
    
    	int planetIndex = 0;
    	double millionsOfMiles;
    	
    	
    	
    	public void init()
    	{
    		setLayout(null);
    		setSize(400, 400);
    
    		/* ------------------- initialization */
    		planet = new JLabel();
    		planet.setBounds(100, 50, 100, 20);
    		planet.setFont(new Font("Default", Font.PLAIN, 12));
    		planet.setText("Select Planet");
    		planet.setForeground(black);
    		planet.setHorizontalAlignment(JLabel.LEFT);
    		add(planet);
    
    		planetCombo = new JComboBox(planetArray);
    		planetCombo.setBounds(210, 50, 100, 20);
    		planetCombo.setForeground(black);
    		planetCombo.setBackground(white);
    		planetCombo.setMaximumRowCount(7);
    		add(planetCombo);
    
    		textA = new JTextArea();
    		textA.setBounds(80, 110, 250, 90);
    		textA.setFont(new Font("Default", Font.PLAIN, 12));
    		textA.setForeground(black);
    		textA.setBackground(white);
    		textA.setEditable(false);
    		add(textA);
    
    		enterJButton = new JButton();
    		enterJButton.setBounds(80, 300, 100, 20);
    		enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		enterJButton.setText("Enter");
    		enterJButton.setForeground(black);
    		enterJButton.setBackground(white);
    		add(enterJButton);
    		enterJButton.addActionListener(this);
    
    		clearJButton = new JButton();
    		clearJButton.setBounds(210, 300, 100, 20);
    		clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		clearJButton.setText("Clear");
    		clearJButton.setForeground(black);
    		clearJButton.setBackground(white);
    		add(clearJButton);
    		clearJButton.addActionListener(this);
    
    	}
    
    	public void actionPerformed(ActionEvent event)
    	{
    		Object obj = event.getSource();
    		if(obj == enterJButton)
    		{
    			getPlanet();
    		}
    		else if(obj == clearJButton)
    		{
    			clearAll();
    		}
    	}
    
    	public void getPlanet()
    	{
    		planetIndex = planetCombo.getSelectedIndex();
    		getDistances();
    	}
    	
    	public void getInformation()
    	{
    		distancee = new Distancee(planetIndex);
    		getDistances();
    	}
    
    	
    	public void getDistances()
    	{
    		millionsOfMiles = distancee.getDistances();
    		displayData();
    	}
    	
    	public void displayData()
    	{
    		textA.append(millionsOfMiles + "\n");
    	}		
    	
    	public void clearAll()
    	{
    		planetCombo.setSelectedIndex(0);
    		textA.setText("");
    	}
    }
    
    class Distancee
    {
    		String[] planetArray = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
    		double[] distanceArray = {48, 68, 155, 365, 746, 1600, 2820, 2660};
    		
    		int indexOfPlanet;
    		int planetDistance;
    		
    		public Distancee(int planetNumber)
    		{
    			indexOfPlanet = planetNumber;
    			getDistances();
    		}
    		
    		public double getDistances()
    		{
    			return distanceArray[indexOfPlanet];
    		}
    }

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

    Default

    Quote Originally Posted by skuzzie View Post
    Im creating an applet where you simply choose a planet from the combo box, hit enter and proceeds to display the output of miles from the array into the textArea.
    It compiles but when I run it, it doesnt do anything and then points me to the line of code below.
    It certainly does something: it throws a NullPointerException on this line:

    Java Code:
       public void getDistances() {
          millionsOfMiles = distancee.getDistances(); // !! ***  NPE here
          displayData();
       }

    So when you see this, you must ask yourself where you ever set the distancee variable to refer to a distancee object?

    Also, next time, please post the error/exception messages here and indicate which line causes them, else it is hard to help you.

    Luck

  3. #3
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Not sure exactly what an NPE actually means. But to answer your reply, isnt my method setup so that it goes down into the helper class for the actual information??

    millionsOfMiles = value in the original class.
    distancee.getDistances = go to helper class(distancee) and get the Distances method where the distance array is.
    Isnt that what its supposed to do??
    Or am I missing something??

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

    Default

    A NullPointerException or NPE means you're trying to de-reference a null object. In English, the error is occurring because you're trying to call a method on a variable that holds no object, that is null. The variable is "distancee", and it's a Distancee variable. So you must trace your code back to see where you set distancee = to a Distancee object before trying to call a method on it. The answer is -- you don't. You need to assign a Distancee object to this variable before you can use it.

  5. #5
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    I actually had it setup right, but in the first method 'getPlanet' I was calling the distance method before the information method.
    Or atleast thats what I changed and its working now.

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

    Default

    Quote Originally Posted by skuzzie View Post
    I actually had it setup right, but in the first method 'getPlanet' I was calling the distance method before the information method.
    Or atleast thats what I changed and its working now.
    I'm glad that you have it working. When I looked at your code, I didn't see anywhere where you were calling the getInformation() method.

    But most importantly, you now know what an NPE represents and how to debug it.

    Cheers.

Similar Threads

  1. Program compiles but wont run to text file...
    By marylanddem in forum New To Java
    Replies: 2
    Last Post: 12-05-2010, 05:05 PM
  2. This program compiles but doesnt run properly!
    By ErikD99 in forum New To Java
    Replies: 5
    Last Post: 12-03-2010, 09:44 PM
  3. Code compiles in/runs in IDE but not on UNIX system
    By Unclejunebug in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 09:35 AM
  4. Compiles in Netbeans, error in textpad
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 05:50 AM
  5. Program Compiles but Buttons do not display
    By ljk8950 in forum AWT / Swing
    Replies: 8
    Last Post: 08-11-2008, 04:41 AM

Posting Permissions

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