Results 1 to 6 of 6
Thread: compiles; no display
- 12-18-2010, 06:44 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
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]; } }
-
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
- 12-18-2010, 07:06 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
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??
-
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.
- 12-18-2010, 07:42 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 18
- Rep Power
- 0
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.
-
Similar Threads
-
Program compiles but wont run to text file...
By marylanddem in forum New To JavaReplies: 2Last Post: 12-05-2010, 04:05 PM -
This program compiles but doesnt run properly!
By ErikD99 in forum New To JavaReplies: 5Last Post: 12-03-2010, 08:44 PM -
Code compiles in/runs in IDE but not on UNIX system
By Unclejunebug in forum New To JavaReplies: 7Last Post: 10-08-2009, 08:35 AM -
Compiles in Netbeans, error in textpad
By Npcomplete in forum New To JavaReplies: 2Last Post: 11-21-2008, 04:50 AM -
Program Compiles but Buttons do not display
By ljk8950 in forum AWT / SwingReplies: 8Last Post: 08-11-2008, 03:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks