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 13 of 13

Thread: Problem with Java Classes, in an applet

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Java Classes, in an applet

    Here is my main piece of 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:

    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!
    Last edited by SashaThompson; October 1st, 2011 at 08:13 PM.


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

    Default Re: Problem with Java Classes, in an applet

    the program errors,
    Please copy and paste here the full text of the error message.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Java Classes, in an applet

    Quote Originally Posted by Norm View Post
    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)

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

    Default Re: Problem with Java Classes, in an applet

    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.
    Last edited by Norm; October 1st, 2011 at 05:25 PM.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Java Classes, in an applet

    Quote Originally Posted by Norm View Post
    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?

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

    Default Re: Problem with Java Classes, in an applet

    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?

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Java Classes, in an applet

    Quote Originally Posted by Norm View Post
    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!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Java Classes, in an applet

    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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

  10. #10
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Java Classes, in an applet

    Quote Originally Posted by Norm View Post
    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
    Hope I did it right!

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

  12. #12
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Java Classes, in an applet

    Quote Originally Posted by Norm View Post
    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...

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with Java Classes, in an applet

    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.

Similar Threads

  1. Java applet programming problem
    By danielparry in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 4th, 2011, 11:31 AM
  2. Classes problem
    By Skyton in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 03:26 PM
  3. Problem in generation of custom classes at web service client
    By vinod.pandey in forum Member Introductions
    Replies: 0
    Last Post: January 10th, 2011, 07:12 AM
  4. Problem to organize my code in classes
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 21st, 2010, 12:13 PM
  5. applet and JPS problem.... please help me
    By rockster14 in forum Java Applets
    Replies: 0
    Last Post: August 7th, 2009, 03:59 PM