0

i had a problem of my program it is process completed but it has runtime exception the java.lang.NullPointerException. Anyone can help me ? this is my code so far .

  import javax.swing.*;
import java.lang.Character;
import java.io.*;
public class CoproHW
{
    public static String c;
    public static int NOc;
    public static String vince;
    public static void main(String args [])throws IOException
    {

        String vince = JOptionPane.showInputDialog("Enter Your File path :");
         c = JOptionPane.showInputDialog("Enter a character");

        briefer();
    }
    public static void briefer()
    {

        for(int v = 1; v<c.length(); v++)
        {
            char x = c.charAt(v);
            if(Character.isSpaceChar(x))
            {
                NOc++;
            }


            char z = c.charAt(v);
            if(Character.isLetter(z))
            {
                NOc++;
            }
        }

        panty();
    }
    public static void panty()
    {
        File file = new File(vince);

        if(!file.exists())
        {
            JOptionPane.showMessageDialog(null,"Wrong file path !");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "The Number of Characters in "+ c +" is "+ NOc);

            try
            {
                RandomAccessFile gui = new RandomAccessFile(file," ");

                gui.writeBytes("The number of Characters in "+ c + " is " +NOc);
                gui.close();
            }

            catch(IOException m)
            {
                System.out.print(m.getMessage());
                System.exit(0);
            }
        }
    }
}
1
  • NOc in an int here, so that cannot be the source of the exception. Please post the stacktrace. I think your intent is to do File file = new File(c); rather than File file = new File(vince); Commented Feb 7, 2014 at 3:46

2 Answers 2

2

Because you didn't assign value to your global vince variable. Instead, you assigned the expected value to a local variable:

String vince = JOptionPane.showInputDialog("Enter Your File path :");

Modify the code to:

vince = JOptionPane.showInputDialog("Enter Your File path :");

And then it will work.

Sign up to request clarification or add additional context in comments.

Comments

0

Remove the String at the main method.

vince = JOptionPane.showInputDialog("Enter Your File path :");

It is a global variable you have been declared.

2 Comments

I'm just curious, did this add anything that wasn't already in the answer that was provided and has upvotes?
Ya, i realize this thing after i post my answer. Dont know there is another answer because when i read this post, there is no answer posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.