java.lang.ClassCastException happened when i run the java code which is about Java collections interface


HOME ยป Java

When i compile the java code,it can not be compiled correctly .but
when i run the program,
Exception in thread "main"
java.lang.ClassCastException:java.lang.StringBuffer cannot be cast to
java.lang.Comparable.


so i have two question :

/>Should the element which is added to the SortedSet or SortedMap must
be sortable? Does this rule also works in any other interfaces in the
java collections framwork

why did the program can be
compiled correctly .but can not run ? what happened while the program
is runing


import java.util.*;


public class
SortMe {
public static void main(String

When i compile the java code,it can not be compiled correctly .but
when i run the program,
Exception in thread "main"
java.lang.ClassCastException:java.lang.StringBuffer cannot be cast to
java.lang.Comparable.


so i have two question :

/>Should the element which is added to the SortedSet or SortedMap must
be sortable? Does this rule also works in any other interfaces in the
java collections framwork

why did the program can be
compiled correctly .but can not run ? what happened while the program
is runing


import java.util.*;


public class
SortMe {
public static void main(String
Java

I'm Facing Issues at the Run time to compile this piece of code
Which is throwing me an error that java.lang.Integer cannot be cast to
Java.lang.Double. I'll more than happy if anyone help me out to
correct this code


double x;
public Double
getMethod() {
HashMap hashmap= new HashMap();

hashmap = SumCal();
List listabc = (List)
hashmap.get("abclist");
int total=(Integer)
hashmap.get("all_total");
x = (Double)
listabc.get(0)*100/total;
return x;
}
Java

I have this error when running sonar with my gradle build
script.


gradle sonarAnalyze


15:41:02.931
[ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with
an exception.
15:41:02.931 [ERROR]
[org.gradle.BuildExceptionReporter]
15:41:02.931 [ERROR]
[org.gradle.BuildExceptionReporter] * What went wrong: />15:41:02.931 [ERROR] [org.gradle.BuildExceptionReporter] Execution
failed for task ':sonarAnalyze'.
15:41:02.934 [ERROR]
[org.gradle.BuildExceptionReporter] > java.util.ArrayList cannot be
cast to java.lang.String
My sonar
sonar {

server {
Java

I have a query like this


Query importQuery =
advertiserDao.getEntityManager().createNativeQuery(
"select
matching_type from group_key where key_id = " + Key.getkeyId()); />String match = (String) importQuery.getSingleResult(); />

The matching_type is a
enum('good', 'bad', 'repared') type. This gives an
exception as following:


java.lang.ClassCastException:
java.lang.Character cannot be cast to java.lang.String
/>

What does it mean, the actual value is a string. How can I solve
this?

Java

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
Development

I am working on upgrading my codebase to Gradle 2.2 and Android
Studio 1.0. I am currently trying to get Robolectric 2.4 working, but
I am experiencing a strange issue when I try and run Unit Tests. The
issue occurs only after a gradle clean; running the test
suite multiple times will produce passing tests (as expected). When I
run the tests after a clean, I get the following error:



java.lang.ClassCastException:
java.lang.NoClassDefFoundError cannot be cast to
java.lang.RuntimeException


I have traced the error back to this call:



Activity activity =
Robolectric.setupActivity(MainActivity.class);


I experience this error whether I use the Robolectric gradle plugin
(org.robolectric:robolectric-gradle-plugin:0.14.0) or the
JC and K Android unit test plugin
(com.github.jcandksolutions.gradle:android-unit-test:2.1.1).



I found this issue referenced on the Robolectric Github, but it
doesn't look like it has been addressed yet:
rel="nofollow">https://github.com/robolectric/robolectric/issues/1385



This issue is also referenced in the android studio unit test
plugin, under 'Trouble Shooting':
rel="nofollow">https://github.com/evant/android-studio-unit-test-plugin



My current sample code is here:
rel="nofollow">https://github.com/KioKrofovitch/robolectric-upgrade-test



I have been able to run the api-android-16 project on the
Robolectric samples without seeing this issue, though api-android-19
and api-android-21 projects fail for other reasons. I cannot see what
they are doing differently such that they do not get this failure.
rel="nofollow">https://github.com/robolectric/robolectric-samples



Has anyone found a workaround to this? Running the tests twice is
not a good workaround for CI tools like Jenkins or Travis.



EDIT: Embedding code samples



My top level build.gradle where I add the JC and K Unit test
library:



buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath
'com.github.jcandksolutions.gradle:android-unit-test:2.1.1'

// NOTE: Do not place your application dependencies here; they
belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
}
}


My project level build.gradle, where I add in robolectric:



apply plugin: 'com.android.application'

repositories {
mavenCentral()
}

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.example.robolectrictest"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles
getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

sourceSets {
main {
jniLibs.srcDirs = ['libs']
}

androidTest {
setRoot('src/androidTest')
}
}
}

// Must be after Android plugin
apply plugin: 'android-unit-test'

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'

//androidTestCompile 'junit:junit:4.10'
//androidTestCompile 'org.robolectric:robolectric:2.4'

// Testing frameworks
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.4'
}


My very basic Activity, everything has been left as the template
creates it:



public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it
is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


My dummy test, where I can't get to my assert due to the
setupActivity() method error:



@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)

public class ApplicationTest {

@org.junit.Test
public void testDummy() throws Exception {

Activity activity =
Robolectric.setupActivity(MainActivity.class);

assertTrue(true);
}

}


EDIT #2:
To run the tests I invoke the following commands from the top level
directory of my project:



./gradlew clean
./gradlew test


I have also tried running the tests outside the gradle wrapper, and
get the same results



gradle clean
gradle test
Web Development

- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow