Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Why I should seperate this into two lines?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have some weird Exception in my code, when I have this line:
int costScore = ((Integer)myHT.get("costScore")).intValue();
It gives me a run time exception: java.lang.Integer
To fix it, I just seperate it into two lines:
Integer tmpCostScore =(Integer)myHT.get("costScore");
int costScore = tmpCostScore.intValue();
then the Exception is gone. Why? Thanks for any input.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Linda Xu:
Hi, I have some weird Exception in my code, when I have this line:
int costScore = ((Integer)myHT.get("costScore")).intValue();
It gives me a run time exception: java.lang.Integer
To fix it, I just seperate it into two lines:
Integer tmpCostScore =(Integer)myHT.get("costScore");
int costScore = tmpCostScore.intValue();
then the Exception is gone. Why? Thanks for any input.


int costScore = (Integer)((Integer)myHT.get("costScore")).intValue();
Should work, the result of "(Integer)myHT.get("costScore")" is not getting converted to Integer in the first case.
HTH,
- Manish
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would be interested in why you are casting
BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short -- are all on the same level and you can't cast sideways
the below works... However, the parent class is number, and I don't think you can cast to an abstract class so maybe you should not cast. What datatype object is your method returning?
* this works, but it is meaningless
Integer y = new Integer(123);
Integer x = (Integer)y;
hope that helps, Tom Bigbee
------------------
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For better readabilty or documentation....

------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linda,
I am not sure why this is not working for you. I tried the following program and it works fine for me.

I am using jdk 1.3.
-AJ
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll agree with ashik on this one. you may need to come back to this program in the future (or someone else may) so you may as well make it as readable as possible. check the coding guidelines on this site for a discussion of this.
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would put into two lines just for the sake of readability. I am working on some code now that was written by someone who no longer works for the company. Its really neat that one of his classes is only ten lines long, but is a headache to read because he puts entire method definitions on one line.
eg.
public String[] getFiles(File f){String[] filelist = f.list();if(filelist!=null)return filelist;}
You get the idea.
-Jason
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see what is unreadable about this line:
int costScore = ((Integer)myHT.get("costScore")).intValue();
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I find nothing unreadable about this line either. However, what happens if get("costScore") returns null? NullPointerException is what. For a robust program, chances are good that you'll want to split long lines like this up for better error checking.
Linda- what exactly was the full error message you got with the code originally? I can't see any reason it would error in the one-line version and not the two-line version. Are you sure you typed it correctly?
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something to be said for readability .... but I do not think that line is terribly difficult to grasp. However, are you sure that the line you posted is exactly the same as the line in your code? I did a quick test and it seems to work fine. Below is the test
import java.util.Hashtable;
public class Test
{
public static void main(String[] args)
{
Hashtable myHT = new Hashtable();
myHT.put( "costScore", new Integer(10) );
int costScore = ( (Integer)myHT.get("costScore") ).intValue();
System.out.println( "Value of costScore is " + costScore );
}
}
output is:
Value of costScore is 10

 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Personally, I find nothing unreadable about this line either. However, what happens if get("costScore") returns null? NullPointerException is what. For a robust program, chances are good that you'll want to split long lines like this up for better error checking.


I agree with Jim on that point. If get("costScore") returns null and/or if your method does not explicitely says that it throws a NullPointerException, the client of your method may be in trouble.
W.
 
You got style baby! More than this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic