Results 1 to 4 of 4
  1. #1
    pacia007 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException.. PLS HELP

    <code>
    import javax.swing.*;


    public class BruteForce {
    private String text;
    private String pattern;

    public BruteForce(){

    int p = pattern.length();
    int t = text.length();
    int match = 0;

    text = JOptionPane.showInputDialog("input text");
    pattern = JOptionPane.showInputDialog("input pattern");

    for(int i=0;i<t;i++){
    for(int k = 0;k<p;k++){
    if (pattern.charAt(k) == text.charAt(i)){
    i++;
    if(k==p-1){
    match++;
    break;
    }
    }
    else{
    break;
    }
    }


    }

    JOptionPane.showMessageDialog(null,match);
    }

    public static void main(String[] args){
    BruteForce marvin = new BruteForce();
    }

    }

    </code>


    Exception in thread "main" java.lang.NullPointerException
    at BruteForce.<init>(BruteForce.java:10)
    at BruteForce.main(BruteForce.java:49)
    Last edited by pacia007; 04-05-2011 at 08:49 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,878
    Rep Power
    8

    Default

    Your variables pattern and text are both null when you're trying to dereference them.

    PS- "pls" is not a word.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pacia007 is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    i'm just a beginner in java so i really don't know what to do next to solve this error...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Java Code:
    public class BruteForce {
       private String text;  // #1
       private String pattern;  // #2
    
       public BruteForce(){
          int p = pattern.length(); // #3
          int t = text.length();  // #4
          int match = 0;
    What Kevin was telling you is that you declare a text variable at #1 and a pattern variable at #2, but you never initialize them -- they're both null since you've not assigned anything into the variables. So then you try to to use them at #3 and #4, but since they're still both null, you'll get a NPE. The solution is to assign them String data. What that data is supposed to be depends on what you're trying to do.

Similar Threads

  1. Replies: 2
    Last Post: 10-19-2010, 04:22 PM
  2. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  3. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  4. Replies: 1
    Last Post: 02-17-2009, 10:54 AM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 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
  •