1.11M Members

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

 
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);
			}
		
		
	}
	
	
}
 
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)

 
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.

 
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)

 
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

 
0
 

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

 
0
 

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

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

or

public ArrayList getDb2LoggedInCrns () {
..
}
 
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.

 
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>();
 
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;
	}
}
 
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.

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article