java.lang.ClassCastException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    java.lang.ClassCastException

    why i get ClassCastExcept ion in this function


    Code:
    private void write_to_file(String wplata_wyplata,double amount){
    	try{
    		
    		
    		
    	BufferedReader in = new BufferedReader(new FileReader("konta//"+NumerKonta+".txt"));
        
        
    	   
        String wczytane_dane="";
        String line = in.readLine();
    
        double sd= sr_dost;
        Double a1 = new Double(sd);
        while (line !=null) {
        	wczytane_dane+=line;
        	line = in.readLine();
        	wczytane_dane+="\n";
        } 
     
        in.close();
      String patternStr = "[0-9]{0,2}\\.{1}[0-9]{0,2}";
      String replacementStr = a1.toString();;
    
    
      Pattern pattern = Pattern.compile(patternStr);
    
    
      Matcher matcher = pattern.matcher(wczytane_dane);
      String output = matcher.replaceFirst(replacementStr);
     
      
    
      BufferedWriter out = new BufferedWriter(new FileWriter("konta//"+NumerKonta+".txt"));
      double a= amount;
      Double a2 = new Double(amount);
      Calendar calendar = new GregorianCalendar();
      Date date = (Date)calendar.getTime();
      DateFormat localFormat = DateFormat.getDateInstance();
     output+='\n'+wplata_wyplata+" "+localFormat.format( date ).toString()+" "+a2.toString();	
    System.out.println(output);
     
     //out.write(output);	
    out.close();
        
        
     
        
        
        
    	  }catch (FileNotFoundException e) {
    		    System.err.println("Nie ma takiego konta(File not found!)");
    	  }catch (IOException e) {
    	          System.err.println(e);
    	  } catch (NumberFormatException e) {
    	         System.out.println("NumberFormatException: " + e.getMessage());    
    	  }catch (Exception ex)  {
        ex.printStackTrace();  }
    	
    	
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    the traceback when you get the Exception should say on which line of code the error happened - what does it say?

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      now it highlights localFormat.for mat(date) as an error can not resolve to a type
      but i declared date

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        that's what it says

        java.lang.Class CastException: java.util.Date cannot be cast to java.sql.Date
        at zad11.Konto.wri te_to_file(Kont o.java)
        at zad11.Konto.dep ozyt(Konto.java )
        at zad11.Konto.acc ess$2(Konto.jav a)
        at zad11.Konto$2.a ctionPerformed( Konto.java)
        at javax.swing.Abs tractButton.fir eActionPerforme d(Unknown Source)
        at javax.swing.Abs tractButton$Han dler.actionPerf ormed(Unknown Source)
        at javax.swing.Def aultButtonModel .fireActionPerf ormed(Unknown Source)
        at javax.swing.Def aultButtonModel .setPressed(Unk nown Source)
        at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Unk nown Source)
        at java.awt.Compon ent.processMous eEvent(Unknown Source)
        at javax.swing.JCo mponent.process MouseEvent(Unkn own Source)
        at java.awt.Compon ent.processEven t(Unknown Source)
        at java.awt.Contai ner.processEven t(Unknown Source)
        at java.awt.Compon ent.dispatchEve ntImpl(Unknown Source)
        at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
        at java.awt.Compon ent.dispatchEve nt(Unknown Source)
        at java.awt.Lightw eightDispatcher .retargetMouseE vent(Unknown Source)
        at java.awt.Lightw eightDispatcher .processMouseEv ent(Unknown Source)
        at java.awt.Lightw eightDispatcher .dispatchEvent( Unknown Source)
        at java.awt.Contai ner.dispatchEve ntImpl(Unknown Source)
        at java.awt.Window .dispatchEventI mpl(Unknown Source)
        at java.awt.Compon ent.dispatchEve nt(Unknown Source)
        at java.awt.EventQ ueue.dispatchEv ent(Unknown Source)
        at java.awt.EventD ispatchThread.p umpOneEventForF ilters(Unknown Source)
        at java.awt.EventD ispatchThread.p umpEventsForFil ter(Unknown Source)
        at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(Unknown Source)
        at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
        at java.awt.EventD ispatchThread.p umpEvents(Unkno wn Source)
        at java.awt.EventD ispatchThread.r un(Unknown Source)

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          to problem appears to be converting from a java.util.Date to a java.sql.Date.
          Have a look at
          http://forum.java.sun. com/thread.jspa?thr eadID=536406&me ssageID=2593330

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by oll3i
            why i get ClassCastExcept ion in this function


            Code:
            private void write_to_file(String wplata_wyplata,double amount){
            	try{
             
             
             
            	BufferedReader in = new BufferedReader(new FileReader("konta//"+NumerKonta+".txt"));
             
             
             
            String wczytane_dane="";
            String line = in.readLine();
             
            double sd= sr_dost;
            Double a1 = new Double(sd);
            while (line !=null) {
            	wczytane_dane+=line;
            	line = in.readLine();
            	wczytane_dane+="\n";
            } 
             
            in.close();
            String patternStr = "[0-9]{0,2}\\.{1}[0-9]{0,2}";
            String replacementStr = a1.toString();;
             
             
            Pattern pattern = Pattern.compile(patternStr);
             
             
            Matcher matcher = pattern.matcher(wczytane_dane);
            String output = matcher.replaceFirst(replacementStr);
             
             
             
            BufferedWriter out = new BufferedWriter(new FileWriter("konta//"+NumerKonta+".txt"));
            double a= amount;
            Double a2 = new Double(amount);
            Calendar calendar = new GregorianCalendar();
            Date date = (Date)calendar.getTime();
            DateFormat localFormat = DateFormat.getDateInstance();
            output+='\n'+wplata_wyplata+" "+localFormat.format( date ).toString()+" "+a2.toString();	
            System.out.println(output);
             
            //out.write(output);	
            out.close();
             
             
             
             
             
             
            	 }catch (FileNotFoundException e) {
            		 System.err.println("Nie ma takiego konta(File not found!)");
            	 }catch (IOException e) {
            	 System.err.println(e);
            	 } catch (NumberFormatException e) {
            	 System.out.println("NumberFormatException: " + e.getMessage()); 
            	 }catch (Exception ex) {
            ex.printStackTrace(); }
             
             
            }
            Like I said before, use GregorianCalend ar for dates.

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              i solved the prob thank u

              Comment

              • brave

                #8
                Could you please explain what u did to resolve this issue ?.

                Thanks
                brave

                Comment

                Working...