• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object or String Problem. Need Help!

 
Ranch Hand
Posts: 158
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody tell me what is wrong into this code.

ERROR: Incompatible Type
Found : java.lang.Object
Required: java.lang.String

error occur at (iRow


////////////////
private void insertLedgerBalance(int iRow)
{
try{
if (optReceipts.isSelected())
{
String atCode = tbl_PettyReceipt.getValueAt(iRow, 0);
String atAmount = tbl_PettyReceipt.getValueAt(iRow, 3);
}

else if(optDeduction.isSelected())
{
for (int a = 0; a < tbl_PettyReceipt.getRowCount(); a++)
{
String atCode = tbl_PettyReceipt.getValueAt(iRow, 0);
String atAmount = tbl_PettyReceipt.getValueAt(iRow, 3);
}
}
}
catch(Exception e){
System.out.println(e);
}
}
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ananad,
Please see correct code:


////////////////
private void insertLedgerBalance(int iRow)
{
try{
if (optReceipts.isSelected())
{
String atCode = (String)tbl_PettyReceipt.getValueAt(iRow, 0);
String atAmount = (String)tbl_PettyReceipt.getValueAt(iRow, 3);
}

else if(optDeduction.isSelected())
{
for (int a = 0; a < tbl_PettyReceipt.getRowCount(); a++)
{
String atCode = (String)tbl_PettyReceipt.getValueAt(iRow, 0);
String atAmount = (String)tbl_PettyReceipt.getValueAt(iRow, 3);
}
}
}
catch(Exception e){
System.out.println(e);
}
}
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anand - the previous post corrected your code but didn't tell you why it is correct. I am presuming that tbl_PettyReceipt is some sort of collection. When you put something into a collection, it is casted up to an Object. So, when you pull it out, you have an Object reference, even though the underlying object is actually a String. So, it has to be cast to a String before it can be assigned to the atCode or atAmount variable. Makes sense?

John
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, All -

Please use the CODE UBB codes when posting code snippets. Doing so makes reading the code much easier as it preserves the indentation.

Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic