Results 1 to 7 of 7
  1. #1
    MS-POWER is offline Member
    Join Date
    Oct 2016
    Posts
    24
    Rep Power
    0

    Default Strange Problem Integer can not be cast to String

    HI

    I have two buttons for same code , first button (Invoice on Hold)will insert data in temp table , second button will insert data in main table ,

    when I insert data from JTable into database temp table or to main table directly its working well and No problem at all , but when I retrieve data from temp and want to save it in the main table I have exception error which I could not find reason for that :


    java.lang.ClassCastException:java.lang.Integr cannot be cast to java.lang.String




    her is the code for both buttons the difference only the table name:


    Java Code:
    //***********Insert Tables Row (Batch) in database ***********
          
              int rows=jTableInvoiceItems.getRowCount();
    
            String queryco = "Insert into invoiceClientItems (Invoice_Number,Item_Number,Item_Name,Employee_Name,Client_Number,Quantity,Price,SubTotal,Percentage,Discount,Total) values (?,?,?,?,?,?,?,?,?,?,?)";
            pst = conn.prepareStatement(queryco);
            for(int row = 0; row<rows; row++)
        {
        pst.setInt   (1, (Integer)parseInt(jInvoiceNumberField.getText()));
        
        pst.setInt   (2, (Integer) jTableInvoiceItems.getValueAt(row, 0));
        pst.setString(3, (String)jTableInvoiceItems.getValueAt(row, 1));
        pst.setString(4, (String)jTableInvoiceItems.getValueAt(row, 2));
        pst.setInt   (5, (Integer)jTableInvoiceItems.getValueAt(row, 3));
        pst.setInt   (6, (Integer)jTableInvoiceItems.getValueAt(row, 4));
        pst.setDouble(7,  (Double)jTableInvoiceItems.getValueAt(row, 5));
        pst.setDouble(8,  (Double)jTableInvoiceItems.getValueAt(row, 6));
        pst.setInt   (9, (Integer) jTableInvoiceItems.getValueAt(row, 7));
        pst.setDouble(10,  (Double)jTableInvoiceItems.getValueAt(row, 8));
        pst.setDouble(11,  (Double) jTableInvoiceItems.getValueAt(row, 9));
        
        pst.addBatch();
    }
        pst.executeBatch();

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Strange Problem Integer can not be cast to String

    java.lang.ClassCastException:java.lang.Integer cannot be cast to java.lang.String
    What source line does that exception happen on?

    The Integer class has methods that will return the object's value as a String. Look at the API doc for the Integer class.
    Last edited by Norm; 11-05-2016 at 08:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Strange Problem Integer can not be cast to String

    You can not cast an integer to a String. It needs to be converted. And you need to show the complete
    error with line numbers that match your cut and paste.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    MS-POWER is offline Member
    Join Date
    Oct 2016
    Posts
    24
    Rep Power
    0

    Default Re: Strange Problem Integer can not be cast to String

    Quote Originally Posted by jim829 View Post
    You can not cast an integer to a String. It needs to be converted. And you need to show the complete
    error with line numbers that match your cut and paste.

    Regards,
    Jim
    thanks a lot

    I got only this exception error window and there is no error shown in output pane , even when I debug this file I didn't get any message in OutPut pane.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Strange Problem Integer can not be cast to String

    exception error window
    How are you executing the program?
    Do all the catch block's call the printStackTrace() method so that you will get the full text of the error messages on the console?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    MS-POWER is offline Member
    Join Date
    Oct 2016
    Posts
    24
    Rep Power
    0

    Default Re: Strange Problem Integer can not be cast to String

    Quote Originally Posted by Norm View Post
    How are you executing the program?
    Do all the catch block's call the printStackTrace() method so that you will get the full text of the error messages on the console?
    thanks a lot printStackTrace() show me where is the problem , I will work to fix it.

    I think I will use printStackTrace() in all try catch Blocks.

    bestregards

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Strange Problem Integer can not be cast to String

    use printStackTrace() in all try catch Blocks.
    Yes, that is highly recommended!!!
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Compile Error: Cannot cast from integer to byte
    By johnmerlino in forum New To Java
    Replies: 2
    Last Post: 05-29-2014, 11:01 PM
  2. Replies: 1
    Last Post: 03-05-2014, 12:48 AM
  3. cast string to treeNode
    By jasonwucinski in forum Advanced Java
    Replies: 1
    Last Post: 04-27-2011, 12:51 AM
  4. Cast From Image to String
    By Deepa in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 03:07 PM
  5. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 02:04 PM

Posting Permissions

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