Problem with Java Classes, in an applet
Here is my main piece of code:
Code :
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MiniMajorProject extends Applet implements KeyListener, ItemListener {
TextField OriginalText;
String politeForm;
boolean isPoliteForm, isPoliteFormpast, PresentisPoliteForm, isPast;
int len;
Checkbox checkbox1, checkbox2;
PoliteForm politeObject = new PoliteForm();
public void init() {
setLayout (null);
OriginalText = new TextField();
OriginalText.setBounds(200, 200, 100,35);
add(OriginalText);
OriginalText.addKeyListener(this);
checkbox1 = new Checkbox("polite form present");
checkbox1.setBounds(350,180,130,35);
checkbox1.addItemListener(this);
add(checkbox1);
checkbox2 = new Checkbox("polite form past");
checkbox2.setBounds(350,250,130,35);
checkbox2.addItemListener(this);
add(checkbox2);
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == checkbox1) {
len = politeForm.length();
politeObject.polite();
}
if (e.getSource() == checkbox2){
if (politeForm.endsWith("iru")){
len = politeForm.length();
isPast = true;
isPoliteForm = false;
}
if (politeForm.endsWith("eru")){
len = politeForm.length();
isPast = true;
isPoliteForm = false;
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
politeForm = OriginalText.getText();
}
this.repaint(); //request a repaint
}//end KeyPressed()
public void paint(Graphics g)
{
if (isPoliteForm)
{
g.drawString(politeForm.substring(0,(len -2)) + "masu", 250,300);
}
if (isPast)
{
g.drawString(politeForm.substring(0,(len -2)) + "mashita", 300,300);
}
}//end paint()
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}//end class
Here is the class PoliteForm:
Code :
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class PoliteForm {
String politeForm;
TextField OriginalText;
boolean isPoliteForm, isPoliteFormpast, PresentisPoliteForm, isPast;
int len;
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
politeForm = OriginalText.getText();
}
}
public boolean polite(){
if (politeForm.endsWith("iru")){
len = politeForm.length();
isPoliteForm = true;
isPast = false;
}
if (politeForm.endsWith("eru")){
len = politeForm.length();
isPoliteForm = true;
isPast = false;
}
return isPoliteForm;
}
}
I can't use checkbox one as the program errors, I think it's because I wrote the class wrong..
Can anybody help and fix my problem?
Thankyou so much!
Re: Problem with Java Classes, in an applet
Quote:
the program errors,
Please copy and paste here the full text of the error message.
Re: Problem with Java Classes, in an applet
Quote:
Originally Posted by
Norm
Please copy and paste here the full text of the error message.
Error Message:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at MiniMajorProject.itemStateChanged(MiniMajorProject .java:36)
at java.awt.Checkbox.processItemEvent(Unknown Source)
at java.awt.Checkbox.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Re: Problem with Java Classes, in an applet
Quote:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at MiniMajorProject.itemStateChanged(MiniMajorProject .java:36)
At line 36 in MiniMajorProject there is a variable with a null value. Look at that line of code and see what variable is there that is null and then backtrack in your code to see why that variable does not have a valid value.
If you can't see which variable is null add a println to show the values of all the variables on that line.
Re: Problem with Java Classes, in an applet
Quote:
Originally Posted by
Norm
At line 36 in MiniMajorProject there is a variable with a null value. Look at that line of code and see what variable is there that is null and then backtrack in your code to see why that variable does not have a valid value.
If you can't see which variable is null add a println to show the values of all the variables on that line.
I found that variable, I deleted the variable len for the purpose of seeing if that got rid of the error and once I deleted it I got the same issue but it said no value was being assigned to politeObject.polite(), there is deffinitely a problem with the class I just can't see what though any ideas?
Re: Problem with Java Classes, in an applet
Quote:
there is deffinitely a problem with the class I just can't see what though
Please explain why you think there is a problem. Do you see an error message or bad output or what?
Re: Problem with Java Classes, in an applet
Quote:
Originally Posted by
Norm
Please explain why you think there is a problem. Do you see an error message or bad output or what?
I think there is an error with the object because it seems that no value is assigned to the object because it is coming up with the same error and that line is where the object is being called, also because I just am not very good with objects and classes so don't know if it is possible to transfer the word inputted in the textfield to the object class, where the object class then has a bunch of if statements which check the ending of the word and then returns a boolean value depending on the end of the word.
I don't know if it's actually possible!
Re: Problem with Java Classes, in an applet
Quote:
it is coming up with the same error
If you are getting a NullPointerException you need to track down why the variable does not have a valid value.
Re: Problem with Java Classes, in an applet
Please edit your post, select the code and wrap it with code tags.
See:BB Code List - Java Programming Forums
Or press the Go Advanced and use the #icon
Re: Problem with Java Classes, in an applet
Quote:
Originally Posted by
Norm
Hope I did it right!
Re: Problem with Java Classes, in an applet
Yes, that is a lot easier to read and understand.
Have you found the null variable and then found out why it doesn't not have a valid value?
Re: Problem with Java Classes, in an applet
Quote:
Originally Posted by
Norm
Yes, that is a lot easier to read and understand.
Have you found the null variable and then found out why it doesn't not have a valid value?
I think the null variable is the boolean, isPoliteForm but I don't know why it doesn't have a valid value...
Re: Problem with Java Classes, in an applet
Quote:
I think the null variable is the boolean
Primitive variables can not have null values. boolean is a primitive.
reference variables can have null values and that will cause the exception.
Add some printlns to print out the values of all the variables used on the statement where the error occurs so you can see which variable has a null value.