1,105,578 Community Members

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

Member Avatar
ashwiniku
Newbie Poster
9 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi, i am getting below error plz help

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at com.kotak.autoblocknb.transaction.Transaction.main(Transaction.java:38)

ArrayList<Integer> db2Crns=new ArrayList<Integer>();		
		ArrayList<Integer> oracleCrns=new ArrayList<Integer>();
		ArrayList<Integer> leftOverCrns=new ArrayList<Integer>();
		db2Crns=testDB2Connect.getDb2LoggedInCrns();
		oracleCrns=testOracleConnection.getDataFromPartyAuditInfo();
		
		for(int i=0;i<db2Crns.size();i++)
		{
			int x=db2Crns.get(i).intValue();
			int y=oracleCrns.get(i).intValue();
			
			
			if(x!=y)
			{
				
				leftOverCrns.add(db2Crns.get(i));
				leftOverCrns.add(oracleCrns.get(i));
			}
			else if(x==y)
			    {
				leftOverCrns.add(oracleCrns.get(i));
				}
			
		}
		int lastIndexOfOne=db2Crns.size();
		for(int i=lastIndexOfOne;i<oracleCrns.size();i++)
			{
				if(leftOverCrns.contains(oracleCrns.get(i)))
				{continue;}
			
				else
				leftOverCrns.add(oracleCrns.get(i));
			}
			
					
		
		
			for(int i=0;i<db2Crns.size();i++)
			{
				
				if(leftOverCrns.contains(db2Crns.get(i)))
				{
					leftOverCrns.remove(db2Crns.get(i));	
				}
				
			}
			
			System.out.println("Leftover CRN are,");
			for(Integer i:leftOverCrns)
			{
				System.out.println(+i);
			}
		
		
	}
	
	
}
Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

The error is very clear:

java.lang.String cannot be cast to java.lang.Integer

You are trying to cast a String object to an Integer. That cannot be done. They are not of the same type.

The error tells you at what line of your file that happened, so you can correct it.

Don't expect us to count all that code line by line, until we find the line where the error occurred:
com.kotak.autoblocknb.transaction.Transaction.main(Transaction.java:38)

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

From snippet you provided it is difficult to find out where you are casting string to integer, but I have feeling that it is somewhere in this loop

for(int i=lastIndexOfOne;i<oracleCrns.size();i++)
			{
				if(leftOverCrns.contains(oracleCrns.get(i)))
				{continue;}
			
				else
				leftOverCrns.add(oracleCrns.get(i));
			}

You referring to oracle connection. Are you sure that this can return you size() which is part of your termination expression?

If that is not case you will need to provide full error as you got it and mark which line it is complaining about.

Member Avatar
ashwiniku
Newbie Poster
9 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i am getting error on this line

int x=db2Crns.get(i).intValue();---- line no 38

i tried to change below line to
int x=((Integer)db2Crns.get(i)).intValue();
but still same error is coming

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at com.kotak.autoblocknb.transaction.Transaction.main(Transaction.java:38)

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

At that line you call:

db2Crns=testDB2Connect.getDb2LoggedInCrns();

does it return an ArrayList that has correct values inside?
I don't believe you should get an error. Can you post what that method does? getDb2LoggedInCrns

Member Avatar
ashwiniku
Newbie Poster
9 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi, in getDb2LoggedInCrns() i took some user_ID from database by using query.

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

Can you post the signature of the getDb2LoggedInCrns method?
Is it like something this?

public ArrayList<Integer> getDb2LoggedInCrns () {
..
}

or

public ArrayList getDb2LoggedInCrns () {
..
}
Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

i am getting error on this line

int x=db2Crns.get(i).intValue();---- line no 38

i tried to change below line to
int x=((Integer)db2Crns.get(i)).intValue();
but still same error is coming

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at com.kotak.autoblocknb.transaction.Transaction.main(Transaction.java:38)

db2Crns.get(i) returns a String and you are trying to cast it to an Integer hence the error. Use Integer.parseInt for parsing an integer from a string.

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

db2Crns.get(i) returns a String and you are trying to cast it to an Integer hence the error. Use Integer.parseInt for parsing an integer from a string.

But 'db2Crns' is declared like this:

ArrayList<Integer> db2Crns=new ArrayList<Integer>();
Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

That still doesn't matter since the line db2Crns=testDB2Connect.getDb2LoggedInCrns(); would screw everything up if the method getDb2LoggedInCrns returns a raw list.

public class Tester {
	
	public static void main(final String[] args) {
		List<Integer> ints = new ArrayList<Integer>();
		ints = getList();
		ints.get(0).intValue();	// KABOOM!!!
	}

	private static List getList() {
		List<String> strings = new ArrayList<String>();
		strings.add("HI");
		return strings;
	}
}
Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

That still doesn't matter since the line db2Crns=testDB2Connect.getDb2LoggedInCrns(); would screw everything up if the method getDb2LoggedInCrns returns a raw list.

public class Tester {
	
	public static void main(final String[] args) {
		List<Integer> ints = new ArrayList<Integer>();
		ints = getList();
		ints.get(0).intValue();	// KABOOM!!!
	}

	private static List getList() {
		List<String> strings = new ArrayList<String>();
		strings.add("HI");
		return strings;
	}
}

Which is why, in one of my previous posts I asked for the code of the method he is calling:

Can you post what that method does? getDb2LoggedInCrns

And then I asked for the signature since the poster didn't post any code after my initial request.
I am still waiting for ashwiniku's response about the method he is calling that returns that list.

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

Which is why, in one of my previous posts I asked for the code of the method he is calling:

Which again is not required since by looking at the error message its pretty clear that it returns a list of strings. :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article