Hi!
My project has two text file:input and output

java.util.NoSuchElementException: No line foundread exception
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:140)
at nfa2dfa.NfaToDfa.OutputFile(NfaToDfa.java:217)
at nfa2dfa.Main.main(Main.java:30)

I don't know what they mean?!

Recommended Answers

All 6 Replies

Hi!
My project has two text file:input and output

java.util.NoSuchElementException: No line foundread exception
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:140)
at nfa2dfa.NfaToDfa.OutputFile(NfaToDfa.java:217)
at nfa2dfa.Main.main(Main.java:30)

I don't know what they mean?!

as for as i can see it says you are trying to write a null value to the textfile, check in your write method and make sure the variable thats throwing the exception gets initialized. maybe with a few println()'s

Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:140)
at nfa2dfa.NfaToDfa.OutputFile(NfaToDfa.java:217)

Look at line 217 where you call the write method and check the values of the variables

line 140 is

public void write(String str) throws IOException {
	write(str, 0, str.length());
    }

(java/io/Writer.java)

can anyone help me if I attach my project here?

Did you ever find What variable is null?
And then check to see why it is null and change the code so the variable would have a valid value.

I've checked my variables...here is complete code
I solved that but there's another one...
Exception in thread "main" java.lang.NullPointerException
java.util.NoSuchElementException: No line foundread exception
at java.io.Writer.write(Writer.java:140)
at nfa2dfa.NfaToDfa.OutputFile(NfaToDfa.java:236)
at nfa2dfa.Main.main(Main.java:32)
Java Result: 1

//import java.lang.String;

package nfa2dfa;
import java.io.*;
import java.util.*;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Emitis
 */
public class NfaToDfa {


        String strtmp=null;
        String strtmp2=null;
        String strtmp3=null;
        String strtmp4=null;
        int i, j;
        String[] Alpha=new String[27];
        String rulenfa=null; // tedad e qanunhaye harkat dar nfa
        String Fstate=null;
        String[] Lstate=new String[100];
        String[][] Rules=new String[100][3];
        String State[]=new String[100];
        String delimiter = ",";
        String s=null;
        String[][] dfa=new String[100][3];
        int[] check=new int[100];
        int n=0;
        int d,t,h;
        int p=0;
        int g=1 ;
        int x=0;
        int sum=0;
        String not="not";
        String[][] converted=new String[100][3];
        String temp=null;
        String[] Lsatateconvereted=new String[100];

       public void inputfile() throws FileNotFoundException {
      try{
        Scanner nfafile=new Scanner(new FileInputStream("input.txt"));
    //    C:/JAVA/Topics.txt
     //   File file=new File("input.txt");
       // Scanner scan=new Scanner(System.in);
       // alefbaye zaban


       strtmp = nfafile.nextLine();
       State = strtmp.split(delimiter);

       strtmp2=nfafile.nextLine();
       Alpha=strtmp2.split(delimiter);

       Fstate=nfafile.nextLine();

       strtmp3 = nfafile.nextLine();
       Lstate = strtmp3.split(delimiter);

       rulenfa=nfafile.nextLine();

       while(nfafile.hasNextLine()==true){
       for(i=0;i<=100;i++){
       strtmp4 = nfafile.nextLine();
       Rules[i] = strtmp4.split(delimiter);
                }

            }

   nfafile.close();

    }

catch (Exception e){
        System.out.println(e+"read exception");

}
}
//***************************************************************************************
 public void converter(){
   // dfa[0][0]=null;
    //dfa[0][0].concat(Rules[0][0]);
     dfa[0][0]=Rules[0][0];
     dfa[0][1]=Rules[0][1];
     n++;
     s=Rules[0][2];
     for( ;Rules[g][0].compareTo(Rules[0][0])==0 && Rules[g][1].compareTo(Rules[0][1])==0;g++){
     //   s.concat(','+Rules[g][2]);
              s=s+','+Rules[g][2];

    }

      dfa[0][2]=s;
      s=null;
//flag 
     int fl=0;
     for(int i=g;i<Rules.length && Rules[i][0]!=null ;i++ ){
  //dfa[n][0]=Rules[i][0];
    //    s=Rules[i-1][0];
        s=Rules[i-1][2];
         for(  ;i<100 && s!=null && i<Rules.length && Rules[i][0].compareTo(Rules[i-1][0])==0 && Rules[i][1].compareTo(Rules[i-1][1])==0;i++){

        s=s+Rules[i][2]+',';
        fl=1;

    }
       // if(searchDfa(s).compareTo(not)==0)
         // s.concat(dfa[n][2]);
    if(fl==1){
      i--;
      fl--;
    }
    else{
            if((i<99 && i<Rules.length-1) && (Rules[i][0]!=null && Rules[i+1][0]!=null)&& (Rules[i][0].compareTo(Rules[i+1][0])!=0 || Rules[i][1].compareTo(Rules[i+1][1])!=0))

       s=Rules[i][2];
    }
        if(Rules[i][0]!=null&& Rules[i][1]!=null && Rules[i+1][0]!=null && Rules[i-1][1]!=null && s!=null )
        if(i+1<Rules.length && (Rules[i][0].compareTo(Rules[i+1][0])!=0 || Rules[i][1].compareTo(Rules[i-1][1])!=0)){
       // if(Rules[i][0]!=null&& Rules[i][1]!=null && s!=null)
        dfa[n][0]=Rules[i][0];//
        dfa[n][1]=Rules[i][1];
        dfa[n][2]=s;
        }
          
        n++;
        s=null;
 }

    converted[0][0]=converted[0][0]+dfa[0][0]; //shorue amaliate convert
    for(int i=0;i<100 && i<converted.length ;i++){
    LandaTrance();//aya state i ba landa be jaiie rafte ya na
    for(int r=0;r<27 &&  r<Alpha.length && Alpha[r]!=null && converted[i][0]!=null ;r++){
    temp=search(converted[i][0],Alpha[r]);//ba tamame alphabet check mikone ke masalan state i be che jahaiie mire
    if(temp.compareTo(not)!=0){
        converted[i][1].concat(converted[i][1]+Alpha[r]);
        converted[i][2].concat(converted[i][2]+","+temp);

                      }
    LandaTrance();
                                          }

                             }
     for(d=0;d<converted.length && converted[d][0]!=null && d<100 ;d++){
     for(int m=0;m<Lstate.length && Lstate[m]!=null && m<100 ;m++){
     if(converted[d][0].contains(Lstate[m])==true ){
       Lsatateconvereted[x]=converted[d][0];
       x++;
       break;
                          }
    }
    }
i=0;
while (converted[i][0]!=null) {
        i++;
     sum=i;
     }

}
 public void LandaTrance(){
  for(p=0;p<100;p++){
      check[p]=0;
  }
  int k=0;
  h=i;
  int flag=0;
  String htc=null;
  //htc=htc+converted[h][0];
  htc=htc+dfa[h][0];
  //converted[h][1]=converted[h][1]+dfa[h][1];
  while(search(htc,"#")!=not){
      for (int j=0;j<100;j++){ 
          if (check[j]==t)
              flag=1;
      }
      if(flag==1)
          break;
      htc=search(htc,"#");
      check[p]=t;
    p++;
    h++;//che taasiri dare ke h ezafe beshe ?
    k++;
    }

  for(int g=0;g<=k ;g++)
    converted[i][0]= converted[i][0]+dfa[check[g]][0];
  }
 //*******************************************************************
   public String search(String q,String s){
          for(t=0;t<100 && dfa[t][0]!=null ;t++)
    {
     if(dfa[t][0].compareTo(q)==0 && dfa[t][1].compareTo(s)==0)


         return dfa[t][2];
      }
     return not;

    }
   //*****************************************************************

//public String searchDfa(String q){
  //  for (int l=0;l<=100;l++)
    //{
      //  if (converted[l][2].compareTo(q)==0)
        //    return converted[l][2];
    //}
    //return not;
    //}
//*******************************************************************

    public void OutputFile( )throws FileNotFoundException, IOException{
        File file=new File("output.txt");
        FileWriter out=new FileWriter(file);
        out.write(converted[0][0]);
        for (int i = 1; i<converted.length && converted[i][0]!=null; i++) {
            out.write(","+converted[i][0]);
        }
        out.write(";\n");
        out.write(Alpha[0]);
        for (int i = 1; i < Alpha.length && Alpha[i]!=null; i++) {
           out.write(","+Alpha[i]);
        }
        out.write(";\n");
        out.write(converted[0][0]);
        out.write(";\n");

        out.write(Lsatateconvereted[0]);
        for (int i = 1; i < Lsatateconvereted.length && Lsatateconvereted[i]!=null; i++) {
            out.write(","+ Lsatateconvereted[i]);
        }
        out.write(";\n");
        out.write(sum);
        out.write(";\n");
        for (int i = 0; i < sum; i++) {
            out.write(converted[i][0]+","+converted[i][1]+","+converted[i][2]);
            out.write(";\n");
        }
        out.flush();
        out.close();
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package nfa2dfa;
import java.io.*;
import java.util.*;


/**
 *
 * @author ahangari
 */
public class Main {
    
    /**
     * @param args the command line arguments
     */
  //delimiter
       // Reader inp=new FileReader("nfa.txt");
 public static void main(String[] args) throws IOException, ClassNotFoundException {
//******************************************************
  // TODO code application logic here
       
       NfaToDfa n2d=new NfaToDfa();
//       File f = new File("input.txt");
   //    System.out.println(f.getAbsolutePath());
       n2d.inputfile();
       n2d.converter();
       n2d.OutputFile();      
}
             }

Exception in thread "main" java.lang.NullPointerException
java.util.NoSuchElementException: No line foundread exception
at java.io.Writer.write(Writer.java:140)
at nfa2dfa.NfaToDfa.OutputFile(NfaToDfa.java:236)

What variable is null on line 236? Find the variable and make sure it has a valid value before trying to use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.