spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

ArrayList is initialized, but still returns NullPointerException when trying .add(string) method


EDIT: i was totally wrong. This sentence is wrong: You cannot set an adapter with an empty ArrayList, even if u initialized it before. The adapter will find no value and throw the exception. so my solution is useless. I will delete it in 2 days if it's useless. I did notice which was the problem later, but someone else already answered. :) EDIT END

You cannot set an adapter with an empty ArrayList, even if u initialized it before. The adapter will find no value and throw the exception.

Try this:

public class ShowTalkActivity extends
Activity implements OnClickListener {

private ListView listView;
ArrayList<String> messages;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_talk);

    Intent intent = getIntent();
    String selected =
intent.getStringExtra("selected");
    setTitle(selected);

    listView = (ListView)
findViewById(R.id.messagesList);
    messages = new ArrayList<String>();
    //adapter = new
ArrayAdapter<String>(this,R.layout.message_list_row,
R.id.messagesList, messages);
    //listView.setAdapter(adapter);

    ((Button)
findViewById(R.id.sendMessage)).setOnClickListener(this);
}

...

@Override
public void onClick(View view) {
    //System.out.println(messages==null);
    messages.add("Anything");
    if (adapter == null) {
      adapter = new
ArrayAdapter<String>(this,R.layout.message_list_row,
R.id.messagesList, messages);         
      listView.setAdapter(adapter);
    }
    else {
      adapter.addAll(messages);
      adapter.notifyDataSetChanged();
    }
}

Hope i understood your needings.


Categories : Java

Related to : ArrayList is initialized, but still returns NullPointerException when trying .add(string) method
R cor(), method="pearson" returns NA, but method="spearman" returns value. Why?
The Pearson correlation coefficient relies on estimating means and (co)variance. Infinite values lead to infinite means and infinite variances, which break computations. Spearman and Kendall correlation coefficients are rank-based, and thus handle sorting just fine with infinite values (but beware of tied values in your samples!). Try: > lix <- is.infinite(vector1) | is.infinite(vector2) &

Categories : R
Parsing JSON of format Map> in android
As you are new to JSON you may want to use GSON library. TO do that add to yuor dependencies in build.gradle and sync compile 'com.google.code.gson:gson:2.3' Supposing json contains your JSON string you can use it like this: import java.lang.reflect.Type; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; // (...) Gson gson = new Gson(); Type type = new TypeToken<Map&l

Categories : Android
I’m using Deserialization method to create an object getting Exception “NullPointerException”?
You need to pass the serialized file as the input to the InputStream and pass its object to ObjectInputStream. TestEncaps object4 = null; FileInputStream inputFileStream = new FileInputStream("file.dat"); ObjectInputStream objectInputStream = new ObjectInputStream(inputFileStream); object4 = (TestEncaps)objectInputStream.readObject();

Categories : Java
How can we add data values to hashmap>
You need to create the ArrayList and fill it with the first element. Then add the list to the hashmap: if (hashmappattern.containsKey(wordkey)){ hashmappattern.get(wordkey).add(wordlist.get(i)); }else{ ArrayList<String> list = new ArrayList<>(); list.add(wordList.get(i)); hashmappattern.put(wordkey, list); }

Categories : Java
Android: Why the Runnable got nullpointerexception when refering the outer class method?
It sounds like your Service is getting destroyed, and so the outer class is no longer available. Since your Service is returning null from onBind, I assume you want to start your service using startService or an intent filter in your manifest. But do you have an onStartCommand method? That is where you are supposed to do the work of your service, and the return value from that function determine

Categories : Java
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda myMap.stream().count() != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.