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 4 of 4

Thread: Exception in thread "main" java.lang.NullPointerException.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    hello all

    Today I wrote a simple java program but it's error will make me crazy!
    the program got this error:
    Exception in thread "main" java.lang.NullPointerException.

    there is only two class in my program (Triple and parse).
    I have an array of the first class type in the second class.(static Triple[][] sentencesTriples= new Triple[...][...]

    when I try to call a function of the first class in the second class, it got the mentioned error message.
    (sentencesTriples[currentRow][currentColumn++].set(tokens[0], tokens[1], tokens[3])

    do you have any idea to solve this problem?
     

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.io.Writer;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.io.Writer;
    import java.util.StringTokenizer;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;



    public class parse{




    static int currentRow=0;
    static int currentColumn=0;
    static int numOfSentences=50;
    static int maxNumOfRelations=40;


    static Triple[][] sentencesTriples= new Triple[numOfSentences][maxNumOfRelations];

    @SuppressWarnings("null")
    public static void main(String[] args){

    currentRow=0;
    currentColumn=0;

    try {

    File fileSourceDependency = new File("C:/Users/green/Documents/NetBeansProjects/parsDependency/collapsed dependency for suppose in M_Tr.txt");

    File outFile = new File("C:/Users/green/Documents/NetBeansProjects/parsDependency/matchSentences.txt");
    BufferedReader inDependency = new BufferedReader(
    new InputStreamReader(
    new FileInputStream(fileSourceDependency), "UTF8"));

    String strEn;

    try {
    Writer out_dependency_match = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF8"));
    while ((strEn = inDependency.readLine()) != null ) {

    if(strEn.isEmpty()){
    currentRow++;
    currentColumn=0;

    strEn=inDependency.readLine();
    }


    String[] tokens=new String[5];
    for(int i=0; i<5;i++) tokens[i]=null;

    StringTokenizer st;
    st = new StringTokenizer(strEn," ");


    for(int i=0;i<5;i++) tokens[i]=st.nextToken();

    System.out.print(currentRow+" "+currentColumn+" "+tokens[0]+ tokens[1]+ tokens[3]+" ");

    sentencesTriples[currentRow][currentColumn++].set(tokens[0], tokens[1], tokens[3]);


    }


    }
    catch (IOException e)
    {
    System.out.println(e.getMessage());
    }
    }
    catch (IOException e)
    {
    System.out.println(e.getMessage());
    }

    for (int i=0;i<20;i++)
    for(int j=0;j< 5;j++){
    System.out.println(sentencesTriples[i][j].getRelation()+" "+sentencesTriples[i][j].getArg1()
    +" "+sentencesTriples[i][j].getArg2());
    }
    System.out.println("**End**");
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,743
    My Mood
    Mellow
    Thanks
    233
    Thanked 710 Times in 696 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException.

    The array sentenceTriples is filled with nulls until you initialize each element:

    sentenceTriples[i][j] = new Triple();

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException.

    Thank you GregBrannon.
    I am thinking why I didn't ask my question sooner!

    Thank you again.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,743
    My Mood
    Mellow
    Thanks
    233
    Thanked 710 Times in 696 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException.

    You're welcome.

Similar Threads

  1. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 5
    Last Post: December 9th, 2012, 01:25 PM
  4. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM