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> 
Ractually is - is it some kind of static class or singleton? As I do not see any initialization of itRorR.idis null, else the NPE should have been thrown within thefindViewById(...)method