Java keeps giving me an error even though there is not red line thingy -
this program made create files in specified directory user inputs. user can specify name , file extension. recent changes code marked. latest thing did code way program can tell if user added extension or of extension valid.
java.lang.exceptionininitializererror caused by: java.lang.nullpointerexception @ filecreator.coolversion.filecreatorcoolversion.<clinit> (filecreatorcoolversion.java:13) exception in thread "main" c:\users\deadm\appdata\local\netbeans\cache\8.2\executor- snippets\run.xml:53: java returned: 1
^error
code:
package filecreator.coolversion; import java.io.file; import java.io.ioexception; import java.util.*; public class filecreatorcoolversion { public static scanner sc = new scanner(system.in); public static boolean success = false; public static string filename; public static string filedir; public static string fileexten = filename.substring(filename.lastindexof(".") + 1); //recent change here public static file file; public static file dir; public static void main(string[] args) throws ioexception { system.out.println("********************************"); system.out.println("* welcome file creator 2.0! *"); system.out.println("********************************"); system.out.println(" "); while(!success) { system.out.println("would create file? y/n?"); string usrans = sc.nextline(); if(usrans.equalsignorecase("y")) { system.out.println("proceeding file creation..."); break; } else if(usrans.equalsignorecase("n")) { system.out.println("exiting program..."); system.exit(0); } else if(!usrans.equalsignorecase("y") || !usrans.equalsignorecase("n")) { system.out.println("that not valid answer! please try again!"); system.out.println(" "); } } while(!success) { system.out.println(" "); system.out.println("please enter valid filename:"); filename = sc.nextline(); if(filename.isempty()) { system.out.println("please enter file name!"); break; } else if(filename.contains("/") || filename.contains(":") || filename.contains("*") || filename.contains("?") || filename.contains("<") || filename.contains(">") || filename.contains("|") || filename.contains("\"") || filename.contains("\\")) { system.out.println("please not include / \\ : * ? \" < > |"); } else if(!filename.contains(".")) { system.out.println("please add apropriate file extensions"); } else if (!fileexten.isempty() || fileexten.contains("[^\\p{isdigit}\\p{isalphabetic}]")) { //recent change here system.out.println("please specify valid file extension!"); } else { system.out.println(" "); system.out.println("file name \"" + filename + "\" chosen"); break; } } system.out.println(" "); system.out.println("where have file saved?"); system.out.println("please enter valid directory"); while(!success) { filedir = sc.nextline(); if(!filename.contains(":")) { system.out.println(" "); system.out.println("please enter valid directory!"); } else if(!filename.contains("\\")) { system.out.println(" "); system.out.println("please enter valid directory!"); } else { system.out.println("file directory \"" + filedir + "\" chosen"); break; } } system.out.println(" "); system.out.println("creating file..."); }
}
i can't tell error coming please me. not @ debugging programs of , 1 of things need work on. please me.
you can't this:
public static string fileexten = filename.substring(filename.lastindexof(".") + 1);
your file name null. inside method after filename populated.
wiki
Comments
Post a Comment