Results 1 to 4 of 4
  1. #1
    arrkaa is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

    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. 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?


    Java Code:
    public class Triple {
                    String relation=null;
    		String arg1=null;
    		String arg2=null;
    		
                    public	String getRelation(){return relation;}
    		public String getArg1(){return arg1;}
    		public String getArg2(){return arg2;}
    		
    		public void set(String r,String a1, String a2){ relation=r; arg1=a1; arg2=a2;}
    		public Triple() {
    			relation=null; arg1=null; arg2=null;
    		}
                    public static void init(){
                    }
        
    }
    
    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
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

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

    Don't just print the Exception messages in lines #121 and #126 but do 'e.printStackTrace()' and the (long) message will tell you exactly which line caused the error.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

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

    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    105
    Rep Power
    0

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

    go find what line your null pointer is on and make sure its not being accessed before its initialized or instansiated

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  2. Replies: 5
    Last Post: 10-11-2011, 12:16 AM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 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
  •