ClassCastException

Ads

 
 

Share on Google+Share on Google+

Janardhan yadav
ClassCastException
1 Answer(s)      6 years and a month ago
Posted in : Java Beginners
import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}

output: Exception in thread "main" java.lang.ClassCastException

plz explain the flow

Ads
View Answers

April 13, 2011 at 4:49 PM


TreeSet is a container which put objects in sorted order. In order to sort data , object being added should be of type Comparable.But the class Person does not implements the Comparable Interface, therefore the classcastexception occurs.

import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person implements Comparable{ 
   Person(int i){}  
    public int compareTo(Object arg0) {  
    return -1;  
    }
}

By implementing Comparable class and its method, you can remove the exception


Ads









Related Tutorials/Questions & Answers:
ClassCastException
ClassCastException  what is the ClassCastException and exp?   ClassCastException occurs, when you try to assign a reference variable...); try { String y = (String)obj; v.add(y); } catch (ClassCastException e
ClassCastException
ClassCastException  what is the ClassCastException and exp?   ClassCastException occurs, when you try to assign a reference variable...); try { String y = (String)obj; v.add(y); } catch (ClassCastException e
Advertisements
ClassCastException
ClassCastException  what is the ClassCastException and exp?   ClassCastException occurs, when you try to assign a reference variable...); try { String y = (String)obj; v.add(y); } catch (ClassCastException e
ClassCastException
ClassCastException  what is the ClassCastException and exp?   ClassCastException occurs, when you try to assign a reference variable...); try { String y = (String)obj; v.add(y); } catch (ClassCastException e
ClassCastException
ClassCastException  what is the ClassCastException and exp?   ClassCastException occurs, when you try to assign a reference variable...); try { String y = (String)obj; v.add(y); } catch (ClassCastException e
ClassCastException
ClassCastException  what is the ClassCastException and exp?   ClassCastException occurs, when you try to assign a reference variable...); try { String y = (String)obj; v.add(y); } catch (ClassCastException e
ClassCastException
the classcastexception occurs. import java.util.*; public class Test1{ public static void main
ClassCastException - Framework
ClassCastException  How to remove the ClassCastException from this validDate()? public Boolean validDate() { // getValueToValidate(); // When I am reading the value, I am getting ClassCastException. Actually I want
ClassCastException - Framework
But this is giving me ClassCastException.  Hi friend, Code to help
why it is throwing classCastException.
why it is throwing classCastException.   public class Redwood extends Tree { public static void main(String[] args) { new Redwood().go(); } void go() { go2(new Tree(), new Redwood()); go2((Redwood) new
java
java  1.Create a class Errorhandle1. Write code that deliberately throws the following exceptions, without using the √Ę‚?¨Ň?throw√Ę‚?¨¬? keyword ClassCastException NullPointerException ArrayIndexOutOfBoundsException
exception
exception  1.Create a class Errorhandle1. `Write code that deliberately throws the following exceptions, without using the ‚??throw‚?? keyword : ClassCastException NullPointerException ArrayIndexOutOfBoundsException
explanation
explanation  public int compareTo(Object Student) throws ClassCastException { if (!(Student instanceof StudentMarks)) throw new ClassCastException("Not a Student!"); int mark
java fundamental question related to string
??); } } Output? a) the pgm will throw ClassCastException b) compilation
SCJP Module-6 Question-22
Given below the sample code : class main{ try { int a= Integer.parseInt("three"); } } Which exception could be handeled by the catch block for above ? 1.  ClassCastException 2.   
SCJP Module-9 Question-15
will be the output of the above code ? 1. ClassCastException at line 39. 2
Class Cast Exception Example in java
(ClassCastException e) { System.out.println("Class cast exception
Java Comparable Example
){ if(!(ob instanceof Person)){ throw new ClassCastException("Invalid object
Java2Html
Eclipse Plugin Bugfix: conversion caused ClassCastException on some text editors
Java - Java Beginners
ClassCastException { if (!(ob instanceof Employee)) throw new ClassCastException("Error"); int sal = ((Employee) ob).getSalary(); return
need program - Java Beginners
ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int studentmarks = ((ShowData) Student).getMarks
writing program - Java Beginners
Player) throws ClassCastException { if (!(Player instanceof ShowData)) throw new ClassCastException("Error"); int averageRuns = ((ShowData
Java - Java Beginners
) throws ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error
JAVA - Java Beginners
int compareTo(Object Student) throws ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int ide
java error - Java Beginners
Student) throws ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int ide = ((ShowData) Student).getId(); return
DNA sequence - Java Beginners
ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int ide = ((ShowData) Student).getId(); return this.id - ide
java - Java Beginners
compareTo(Object Student) throws ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int ide = ((ShowData
Alphabetical order - Java Beginners
; } public int compareTo(Object Student) throws ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error
java - Java Beginners
; } public int compareTo(Object Student) throws ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int ide
Sort file data and write into another file
ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int ide = ((ShowData) Student).getId(); return
Display Sorted data in TextArea
int compareTo(Object Student) throws ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int ide
Tutorials   
Java Spring Hibernate Struts Training What type of programming language is Java? How do beginners learn about Java? ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017

Ads

 
Advertisement null

Ads