Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: Cannot invoke toString() on the primitive type int

  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Cannot invoke toString() on the primitive type int

    Hi All,

    I am trying to create an applet; when a user clicks on the button the value on the button increases by 1 and getting the error:- "Cannot invoke toString() on the primitive type int"

    Java Code:
    /*<applet code="incrButton.class" width="420" height="380"></applet>*/
    public class incrButton extends java.applet.Applet implements java.awt.event.ActionListener
    {
    	java.awt.Button b1;
    	
    	incrButton()
    	{
    		b1 = new java.awt.Button();
    		b1.setLabel("0");
    		add(b1);
    		b1.addActionListener(this);
    	}
    	
    	public void actionPerformed(java.awt.event.ActionEvent e)
    	{
    		java.awt.Button b = (java.awt.Button) e.getSource();
    		b.setLabel(((Integer.parseInt(getName()))+1).toString());
    		
    	}
    }
    The applet I am trying to create is like this :- Cannot invoke toString() on the primitive type int-screenshot-2012-04-02-14-30-12.png


    Now when the user clicks on the button with label "0" the value must be incremented by 1 with every click.
    Please help, thanks

    Ankit
    Last edited by ankiit; 04-02-2012 at 11:02 AM.

  2. #2
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Cannot invoke toString() on the primitive type int

    Hello All,

    Request you to please share your views about the problem.

    Thanks in advance

    Ankit

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Cannot invoke toString() on the primitive type int

    It's telling you exactly what's wrong.
    Why are you calling toString() on an int?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Cannot invoke toString() on the primitive type int

    Hi All,

    Thanks I got the resolution. Thanks.

    The code that worked for me is as follows:-

    Java Code:
    public class incrButton extends java.applet.Applet implements java.awt.event.ActionListener
    {
    	java.awt.Button b1;
    	
    	public incrButton()
    	{
    		b1 = new java.awt.Button();
    		b1.setLabel("0");
    		add(b1);
    		b1.addActionListener(this);
    	}
    	
    	public void actionPerformed(java.awt.event.ActionEvent e)
    	{
    		java.awt.Button b = (java.awt.Button) e.getSource();
    		int s = ((Integer.parseInt(b.getLabel()))+1);
    		String s1 = String.valueOf(s);
    		b.setLabel(s1);	
    	}
    }
    The working of the applet is shown in 2-3 attachements as follows:-
    Cannot invoke toString() on the primitive type int-22.pngCannot invoke toString() on the primitive type int-2.pngCannot invoke toString() on the primitive type int-1.png

  5. #5
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Cannot invoke toString() on the primitive type int

    Hi Tolls,

    I wanted to increment the label on the button , but it was string; so i converted it to int first and then incremented it and again converted to String.

    Please share if this can be improved.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Cannot invoke toString() on the primitive type int

    You could simply hold a counter in the class and then assign that to the label on the button.
    Increment it each time it's clicked.
    That would save you parsing from a String to an int.
    But the above code works, so it's not a bad solution.
    ankiit likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 04-19-2011, 04:32 PM
  2. non primitive arrays
    By frosty in forum New To Java
    Replies: 2
    Last Post: 08-27-2010, 02:55 AM
  3. Primitive data type and class
    By Roselicious in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 03:27 PM
  4. JNI accessing non primitive data type
    By H_P in forum Advanced Java
    Replies: 1
    Last Post: 04-14-2010, 05:43 AM
  5. Uisng primitive type values as keys for Hashtable
    By ravian in forum New To Java
    Replies: 3
    Last Post: 11-21-2007, 10:13 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
  •