Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: i'm new to java. whats is wrong in this code?

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i'm new to java. whats is wrong in this code?

    OTS.java:

    package ots;
     
    import java.io.File;
    import java.io.IOException;
    public class OTS {
     
    	public static void main(String[] args) throws IOException {
    		CopyDir cp = new CopyDir();
    		cp.copyDir(new File(args[0]), new File(args[1]), new File(args[2]));
    	}
     
    }

    CopyDir.java:
    package ots;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    public class CopyDir {
     
    	public CopyDir() {
     
    	}
     
    	public void copyDir(File sourceLocation, File targetLocation,
    			File tempLocation) throws IOException {
     
    		if (sourceLocation.isDirectory()) {
    			if (!tempLocation.exists()) {
    				tempLocation.mkdir();
    			}
     
    			String[] children = sourceLocation.list();
    			for (int i = 0; i < children.length; i++) {
    				copyDir(new File(sourceLocation, children[i]), new File(
    						tempLocation, children[i]), null);
    			}
    		} else {
     
    			InputStream in = new FileInputStream(sourceLocation);
    			OutputStream out = new FileOutputStream(tempLocation);
     
    			// Copy the bits from instream to outstream
    			byte[] buf = new byte[1024];
    			int len;
    			while ((len = in.read(buf)) > 0) {
    				out.write(buf, 0, len);
    			}
    			in.close();
    			out.close();
    		}
    		tempLocation.renameTo(targetLocation);
    	}
     
    }

    error:

    igor@dual:~$ java -jar ots.jar /home/igor/test /home/igor/123 /home/igor/temp
    Exception in thread "main" java.lang.NullPointerException
    	at ots.CopyDir.copyDir(CopyDir.java:20)
    	at ots.CopyDir.copyDir(CopyDir.java:26)
    	at ots.OTS.main(OTS.java:9)


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i'm new to java. whats is wrong in this code?

    Fixed.

    copyDir(new File(sourceLocation, children[i]), new File(
    tempLocation, children[i]), null);
    changed to:

    copyDir(new File(sourceLocation, children[i]), new File(targetLocation, children[i]), new File(
    tempLocation, children[i]));

Similar Threads

  1. A question About Java (whats my next Step?)
    By DarrenReeder in forum Java Theory & Questions
    Replies: 4
    Last Post: December 7th, 2009, 02:51 AM
  2. whats wrong with this one....
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 6th, 2009, 10:08 AM
  3. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM
  4. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM