Mufasa King
Mufasa King

Reputation: 1

Java keeps giving me an error even though there is not red line thingy

This program is made to create files in a specified directory the user inputs. The user can also specify the name and the file extension. The most recent changes to the code will be marked. The latest thing I did was code a way so that the program can tell if the user added a extension or of the extension is valid.

java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
    at 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 to File Creator 2.0! *");
    System.out.println("********************************");
    System.out.println(" ");

    while(!success) {

        System.out.println("Would you like to create a file? Y/N?");
        String usrans = sc.nextLine();

        if(usrans.equalsIgnoreCase("y")) {

            System.out.println("Proceeding with 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 is not a valid answer! Please try again!");
            System.out.println(" ");
        }
    }

    while(!success) {

        System.out.println(" ");
        System.out.println("Please enter a valid filename:");
        filename = sc.nextLine();

        if(filename.isEmpty()) {

            System.out.println("Please enter a 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 do not include / \\ : * ? \" < > |");

        } else if(!filename.contains(".")) {

            System.out.println("Please add a apropriate file extensions");

        } else if (!fileexten.isEmpty() || fileexten.contains("[^\\p{IsDigit}\\p{IsAlphabetic}]")) { //RECENT CHANGE HERE

            System.out.println("Please specify a valid file extension!");

        } else {

            System.out.println(" ");
            System.out.println("File name \"" + filename + "\" chosen");
            break;
        }
    }

        System.out.println(" ");
        System.out.println("Where would you like to have your file saved?");
        System.out.println("Please enter a valid directory");

    while(!success) {

        filedir = sc.nextLine();

        if(!filename.contains(":")) {

            System.out.println(" ");
            System.out.println("Please enter a valid directory!");

        } else if(!filename.contains("\\")) {

            System.out.println(" ");
            System.out.println("Please enter a valid directory!");

        } else {

            System.out.println("File directory \"" + filedir + "\" chosen");
            break;
        }
    }

    System.out.println(" ");
    System.out.println("Creating file...");

}

}

I can't tell where the error is coming from please help me. I am not that good at debugging programs as of now and it one of the things I need to work on. Please help me.

Upvotes: 1

Views: 58

Answers (2)

Francis Nduba Numbi
Francis Nduba Numbi

Reputation: 3050

filename is still null. So do this first:

public static String fileexten;

Then initialize immediately after filename is given a value:

while(!success) { 
System.out.println(" ");
 System.out.println("Please enter a valid filename:"); 
filename = sc.nextLine();
fileexten = filename.substring(filename.lastIndexOf(".") + 1);

Upvotes: 0

Zaki Anwar Hamdani
Zaki Anwar Hamdani

Reputation: 499

You can't do this:

public static String fileexten = filename.substring(filename.lastIndexOf(".") + 1);

Your file name is null. Do it inside method after filename is populated.

Upvotes: 2

Related Questions