Robert Simoes Robert Simoes - 5 months ago 11x
Java Question

Android: Create new object and manipulate/call new methods serially with Synchronized

I am attempting to have a user create a new Flow object and add it to an ArrayList to keep track when they press then

on the tool bar.

I am struggling with the multithreading of Java, because my methods that require the object and its properties, are running before the object is instantiated caused all sorts of problems

I want my methods to execute serially (ie. show dialog, get name, use object constructor, add new object to list) which is why I've attempted to use the
action on an object which I declared but did not instantiate.

This strategy can't seem to work because the object locked onto cannot be null.

java.lang.NullPointerException: Null reference used for synchronization (monitor-enter)

Any thoughts on how I could make my methods run in serial like this pseudo code:

private Flow newFlow; //Blank flow object declared.
private static List<Flow> flowsInStream = new ArrayList<Flow>();

synchronized (newFlow) {
// presents user a dialog box to receive input.
// takes user input, invokes separate method to actually instantiate
// the newFlow object using the user input.
// Originally blank newFlow object now has:
// = userInput
// --X END X--
// adds the newly instantiated newFlow object to the flowsInStream
// array to keep track of them.
// --X END X--
// displays log message showing both the & the current
// elements in the flowsInStream array.
// --X END X--
} // end of synchronized

public class TheStream extends AppCompatActivity {

private static final String TAG = TheStream.class.getName();
private Toolbar streamToolbar;
private Flow theFlow; //Blank flow object declared.

private static List<Flow> flowsInStream = new ArrayList<Flow>();

protected void onCreate(Bundle savedInstanceState) {

streamToolbar = (Toolbar) findViewById(;

public boolean onPrepareOptionsMenu(final Menu menu) {
getMenuInflater().inflate(, menu);

return super.onCreateOptionsMenu(menu);

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
// User chose the "Settings" item, show the app settings UI...
return true;



return true;

// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);


public void flowDialog() {
//Creates dialog box asking for name for the new flow
AlertDialog.Builder newFlowDialog = new AlertDialog.Builder(TheStream.this);

LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(

//Create edit text field for name entry
final EditText nameInputET = new EditText(TheStream.this);

//Sets maximum length of the EditText
nameInputET.setFilters(new InputFilter[]{new InputFilter.LengthFilter(30)});

//Adds the ET and params to the layout of the dialog box
layout.addView(nameInputET, params);

newFlowDialog.setTitle("Name your new Flow.");


newFlowDialog.setPositiveButton("Lets Roll",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {
if (nameInputET.getText().toString().equals("")) {

Toast.makeText(TheStream.this, "Every Flow deserves a good name :(", Toast.LENGTH_LONG).show();

flowDialog(); //Recall the dialog

} else {
// Sets name of flow object
theFlow = instantiateFlow(nameInputET.getText().toString());

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

//Display Alert;


protected Flow instantiateFlow(String userInput) {
//Instantiates (Constructor) the newFlow object.

Flow newFlow = new Flow(userInput);
Log.d(TAG, "Your flow's name is " + newFlow.getFlowName());
/** Returns errors attached below */
return newFlow;

public void addToStream(Flow flow) {

public void executedCorrectly() {
Log.d(TAG, "The synchronized activity executed correctly because the new Flow object's name is " + theFlow.getFlowName());
Log.d(TAG, "The new Flow list is also updated check it out: " + flowsInStream);

public class Flow {

private String flowName;

public Flow() {

} // End of default constructor

public Flow(String flowName) {
this.flowName = flowName;
} // End of constructor

/** Getters & Setters **/
public void setFlowName(String flowName) {
this.flowName = flowName;
public String getFlowName() {
return this.flowName;

If any additional code would help, just let me know and I'd be happy to post some. And if possible in your answer please mention where my technical understanding was lacking in attempting this.


java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String nhacks16.flow.Main.Flow.getFlowName()' on a null object


Yoy are using synchronized (newFlow), when newFlow is still null. You can't use synchronized on a null reference. If you really want to synchronize, create a different Object (any Object will do) and synchronize on that one, or synchronize on this (just using synchronized { without parenthesis). Which one is correct, depends on what kind of parallelism you want to guard against, which brings me to the next point:

I don't see any multithreading, so I'm not sure, if you even need synchronization.