• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object or String Problem. Need Help!

 
Anand Karia
Ranch Hand
Posts: 156
Java
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Bikramjit Singh Bajwa
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
John Uhlig
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • 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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!