Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Why am I getting the error: Incompatible Type

  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default Why am I getting the error: Incompatible Type

    So I am going through the Java tutorial on Oracle' site and I have hit another bump.

    Java Code:
    import java.io.File;
    import java.io.*;
    import java.util.Date;
    
    public class Test {
    	public static void main(String[] args) throws NullPointerException, IOException {
    		
    	
    	//Testing the setLastModified()
    	File e =	new File("xanadu.txt").setLastModified(new Date().getTime());
    	}
    }
    I cut out the unimportant part. The question is why am I getting the error: incompatible type:

    Java Code:
     ----jGRASP exec: javac -g Test.java
    
    Test.java:66: error: incompatible types
    	File e =	new File("xanadu.txt").setLastModified(new Date().getTime());
    	        	                                      ^
      required: File
      found:    boolean
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    I copied this code from the tutorial after thinking that I typed it wrongly and somehow missed something.

    Would you point me in the right direction please? Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    Read the API doc for the setLastModified() method. What does it return?
    What is on the left side of the assignment operator?

    Break your chained method calls up into single steps.

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    Sweet. You're the man Norm. It worked. See code below.

    Java Code:
    File e =	new File("xanadu.txt");
    	
    	boolean ex = e.setLastModified(new Date().getTime());
    	
    	System.out.println(ex);
    I am now wondering what would be the way to chain the methods and get the desired result? The tutorial had it as one last, but There was no declaration, so I don't know the type on the left of the left hand side.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    what would be the way to chain the methods and get the desired result?
    I personally do NOT like chaining. It doesn't save you any execution time and it can be confusing and cause problems like you just had.
    bigsonny likes this.

  5. #5
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

Similar Threads

  1. Replies: 1
    Last Post: 07-27-2010, 06:01 PM
  2. incompatible types error
    By magic in forum New To Java
    Replies: 3
    Last Post: 06-02-2010, 04:58 PM
  3. Help in incompatible type JTextArea
    By sj_420 in forum Advanced Java
    Replies: 4
    Last Post: 04-15-2010, 03:00 AM
  4. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  5. Replies: 6
    Last Post: 10-08-2008, 08:09 AM

Posting Permissions

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