Results 1 to 11 of 11
  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Exception in inner classs Need Help!!

    Hey i want to add an activity with button that perform two actions and frame and buttons are made in constructor and it is linked with last frame
    1. close the frame (this is done !!)
    2. create a new file

    1 is done but having problem in 2 please help

    the problem is that it shows IOException and i can't understand where to put throws or try and catch please help!!

    Java Code:
    login.addActionListener(new ActionListener(){
    public void addActionPerformed(ActionEvent e)throws IOException{
    if(usern.equals(username) && passw.equals(password){
    File file=new File("C:/file.txt");
    file.createNewFile();
    frame.dispose();
    }
    }
    });
    Last edited by vknehra10; 04-19-2017 at 01:15 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Exception in inner classs Need Help!!

    where to put ... try and catch
    Wrap the method that can throw the exception.
    See the tutorial: Catching and Handling Exceptions (The Java™ Tutorials > Essential Classes > Exceptions)

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: Exception in inner classs Need Help!!

    bro can you please add in given code or write a short code please...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Exception in inner classs Need Help!!

    Please read the tutorial to see how to do it.
    What have you tried?

    Note: The posted code has lost all its formatting. Logically nested statements should be indented.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: Exception in inner classs Need Help!!

    Exception in inner classs Need Help!!-1st.png

    Exception in inner classs Need Help!!-2nd.png

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Exception in inner classs Need Help!!

    Note the word "NOT" in the error message. Use try{} catch
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: Exception in inner classs Need Help!!

    where to put try and catch on which statement after putting try catch can my file will create

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Exception in inner classs Need Help!!

    put the try catch so that the method that throws the exception is inside them.

    Note: Code can not be copied from an image to use in a response. Please do not post images.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: Exception in inner classs Need Help!!

    Java Code:
    login.addActionListener(new ActionListener(){
    				public void actionPerformed(ActionEvent  e)throws IOException{
    					
    			             String usern=jt_usr.getText();
    			                 String passw=String.valueOf(jp.getPassword());
    								
    					 if(usern.equals(username)&& passw.equals(password)){	
                           
    					   File file=new File("C:/file.txt");
    					   file.createNewFile();
    						
                          lgn.dispose();
    					
    					  //MainF mf=new MainF();
    					   }
                             else{
    						 JOptionPane.showMessageDialog(f,"Incorrect Details Try Again !!");
    						 }
    					  
    					
    				}
    				
    			});

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Exception in inner classs Need Help!!

    Does that code compile ok? If not you need to copy the full text of the error message you need help with and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Exception in inner classs Need Help!!

    That's the same code as in the image posted earlier.

    The method that throws the exception is the createNewFile method.
    You need to handle the exception thrown from that method.
    You can't add IOException to the actionPerformed method, as the parent interface does not declare any exceptions being thrown, so all you can do is wrap the call and any other relevant code in a try/catch block (as described in Norm's link post #2).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 5
    Last Post: 05-11-2014, 08:42 PM
  2. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  3. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  4. How Add new classess to the System Classs loader.
    By echarish in forum Advanced Java
    Replies: 0
    Last Post: 02-02-2010, 10:54 AM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •