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


Dmitry
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

Related to : java.lang.ClassCastException happened when i run the java code which is about Java collections interface
java.lang.ClassCastException happened when i run the java code which is about Java collections interface
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
Gradle - Execution failed for task 'sonarAnalyze' - java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
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 {

Retrieving SQL enum causes java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.String
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?


Robolectric 2.4: java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException
20/12/2014 6:00 pm Java

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: https://github.com/robolectric/robolectric/issues/1385

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

My current sample code is here: 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. 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



HD Wallpapers
3D
3D Abstract
City
Celebrities
Indian Celebrities
Cars
Travel
Girls
Animals Birds
Movies
Sports
Black White
Nature
Planes
Anime
Food Drink
Dreamy Fantasy
Architecture
Games
Space
Holidays
Flowers
Love
Artistic
Baby
Beach
Bikes Motorcycles
Macro
Computers
Vector
Funny
Army
Textures
Brands
Misc
Music
Other
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .