0

I use the org.apache.commons.io.FileUtils.readFileToByteArray method in myAndroid application. I imported the jar file and I see this method is in my project. (I use Eclipse.) Actually, compilation is ok. Here's the message from LogCat:

01-26 18:43:08.177: I/dalvikvm(897): Could not find method org.apache.commons.io.FileUtils.readFileToByteArray, referenced from method com.example.anagrams.MainActivity.readFile 

So this is not an error, but then I get a NullPointerException due, apparently, to the statement:

private Button button_read_file = (Button) findViewById(R.id.button_readfile); 

I have no idea how to correct the NullPointerException. Also I am puzzled by the message about the method not found. The application is supposed to read a text file containing English words and then for each word to find all the anagrams that exist. For the moment I just use System.out.println to write the anagrams.

Any help is welcome. Following is my whole MainActivity (the only one so far):

public class MainActivity extends Activity { private String[] words; private Button button_read_file = (Button) findViewById(R.id.button_readfile); private EditText input_file_name = (EditText) findViewById(R.id.editText1) ; private Button button_write_file = (Button) findViewById(R.id.button_writefile); private EditText output_file_name = (EditText) findViewById(R.id.editText2) ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_read_file.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { String inputFileName = input_file_name.getText().toString(); try { words = readFile(inputFileName, Charset.defaultCharset()); } catch (FileNotFoundException e) { e.getStackTrace(); } catch (IOException ioe) { ioe.getStackTrace(); } }}); button_write_file.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { for (int i = 0; i < words.length; ++i) { String s = words[i]; System.out.println(" words[" + i + "] = " + s); HashSet<String> a = new HashSet<String>(); permutations(s,a); Iterator<String> iterator = a.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } } }}); } public String[] readFile(String path, Charset encoding) throws IOException { File file = new File(path); byte[] encoded = FileUtils.readFileToByteArray(file); String s = encoding.decode(ByteBuffer.wrap(encoded)).toString(); s = s.replace(" ",""); return s.split("[,.\\n\\s]"); } public ArrayList<String> getDictionary(String filename) throws IOException, FileNotFoundException { ArrayList<String> dictionary = new ArrayList<String>(); FileReader fileReader = new FileReader(filename); BufferedReader bufferedReader = new BufferedReader(fileReader); String line = null; while ((line = bufferedReader.readLine()) != null) { dictionary.add(line); } bufferedReader.close(); return dictionary; } public boolean isInDictionary(String s) { String dictionaryname = "enable1.txt"; ArrayList<String> dictionary = new ArrayList<String>(); try { dictionary = getDictionary(dictionaryname); } catch (FileNotFoundException e) { e.getStackTrace(); } catch (IOException e) { e.getStackTrace(); } if (dictionary.contains(s)) return true; else return false; } public void permutations(String word, HashSet<String> anagrams) { generatePermutations("",word,anagrams); } public void generatePermutations(String prefix, String word, HashSet<String> anagrams) { int n = word.length(); if (n == 0) { if (prefix != word && isInDictionary(prefix)) anagrams.add(prefix); } else { for (int i = 0; i < n; ++i) { generatePermutations(prefix + word.charAt(i),word.substring(0,i) + word.substring(i+1),anagrams); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } 

}

And here's the layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button_readfile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="17dp" android:text="@string/read_file" /> <Button android:id="@+id/button_writefile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button_readfile" android:layout_below="@+id/button_readfile" android:layout_marginTop="24dp" android:text="@string/write_file" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button_readfile" android:layout_toRightOf="@+id/button_writefile" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button_writefile" android:layout_alignBottom="@+id/button_writefile" android:layout_alignLeft="@+id/editText1" android:ems="10" > </EditText> 

7
  • Are you sure the buttons id you've included in your code is in the same layout? Commented Jan 27, 2014 at 3:03
  • Yes, in the same layout. There's only one layout file actually. Commented Jan 27, 2014 at 3:07
  • I've no experiences with android programming so far, so I'm a bit unaware of what R actually is - is it some kind of static class or singleton? As I do not see any initialization of it Commented Jan 27, 2014 at 3:10
  • R is a Java class generated automatically for each Android application. Commented Jan 27, 2014 at 3:12
  • and it is automatically injected into your class and it has a non-null id? As you indicated that the NPE is thrown on initializing the members, it seems that either R or R.id is null, else the NPE should have been thrown within the findViewById(...) method Commented Jan 27, 2014 at 3:13

1 Answer 1

1

You are initializing these before the layout is even set:

private Button button_read_file = (Button) findViewById(R.id.button_readfile); private EditText input_file_name = (EditText) findViewById(R.id.editText1) ; private Button button_write_file = (Button) findViewById(R.id.button_writefile); private EditText output_file_name = (EditText) findViewById(R.id.editText2) ; 

Change them to:

private Button button_read_file; private EditText input_file_name; private Button button_write_file; private EditText output_file_name; 

Then declare them after setContentView(...) in your onCreate like:

button_read_file = (Button) findViewById(R.id.button_readfile); input_file_name = (EditText) findViewById(R.id.editText1) ; button_write_file = (Button) findViewById(R.id.button_writefile); output_file_name = (EditText) findViewById(R.id.editText2) ; 

If you see NoClassDefFoundError, try this in Eclipse:

  • Go to Project PropertiesJava Build PathOrder and Export tab.
  • Check the Android Private Libraries option.

Android Private Libraries option

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

6 Comments

Yes, you are right. I made a simple mistake. Only that now I have some problems with Eclipse and I cannot run the application. I re-started Eclipse and I am waiting to see if the problem is gone. Nevertheless I am voting up your answer. Thank you!
Now the NullPointerException is gone, but I get a problem with the NoClassDefFoundError: org.apache.commons.io.FileUtils which it is in the Referenced Libraries. Do I need to do some additional work after I added the commons-io-2.4.jar file?
Updated answer. Also make sure your jar is in the libs folder.
I have now the same jar file commons-io-2.4.jar both in the libs folder and in the Android Private Libraries. Is this a mistake? It looks like a method from this jar file does not work, because my application does not do anything after I call this method. And I don't get any error.
Not really no, eclipse will reference the jar only once, thus it will overwrite one with the other. Also for that method you can check if it has an api restriction.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.