Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i've got a little problem with my final project in Android and would really appreciate if someone could help.

I made a Parse.com database, with a "Sessions" table where I create a new line everytime a new Session from my app gets startet. In this line there's a column which gets an Integer-Array.

My issue is how to get single array values from this array.

So here's my Start-Activity, which puts the new object into my parse.com-table:

package de.lichtenberger.gottschalk.android;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class Liedwahl_Git extends Activity {




    private ArrayList<ParseObject> list;
    private ArrayList<SongData> details;
    private SongDataAdapter adapter;
    private ListView songListView;
    public Button ok;
    private EditText sessionText;
    public Boolean[] ableToPlay = new Boolean[30];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liedwahl);

        Parse.initialize(this, "hGxasGU6e0WQAOh5JIOGDfvFBKrYyBJKXIzxBfAG", "WsOPsXerpsFjsjekKKbZnnjAHvXy5PQHVQEB8Cqu"); 
        setupUI();
        new createListTask().execute();
        for(int i = 0; i<=29; i++){
            ableToPlay[i] = false;
        }



    }

    private void setupUI() {
        songListView = (ListView) findViewById(R.id.listeCheck);
        ok = (Button)findViewById(R.id.OkButton);
        ok.setVisibility(View.INVISIBLE);
    }

    private void setupOnClickListeners() {

        sessionText = (EditText)findViewById(R.id.newSession);

        ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(sessionText.getText().toString().isEmpty()){

                    Toast.makeText(getApplicationContext(), "Bitte Sessionname angeben!", Toast.LENGTH_SHORT).show();

                }else{


                    ParseObject session = new ParseObject("Sessions");
                    session.put("Name", sessionText.getText().toString());
                    session.put("Online", true);


                    for(int i = 0; i<=29; i++){

                        if(ableToPlay[i]==true){
                            session.addUnique("AbleToPlay", i);
                        }
                    }

                    session.saveInBackground();



                    Intent lagerfeuer = new Intent(Liedwahl_Git.this, Lagerfeuer_Lieder.class);


                    lagerfeuer.putExtra("ParseOb", sessionText.getText().toString());
                    Log.d("Parse", sessionText.getText().toString());
                    startActivity(lagerfeuer);


                }


            }
        });







    }

    private class createListTask extends AsyncTask<Void,Void,Integer>{

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(Liedwahl_Git.this);
            dialog.setMessage("Please wait, while loading!");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.show();


        }


        @Override
        protected Integer doInBackground(Void... arg0) {

            list = new ArrayList<ParseObject>();
            ParseQuery query = new ParseQuery("SongDatenbank");
            details = new ArrayList<SongData>();
            query.orderByAscending("IDS");

            query.findInBackground(new FindCallback() {
                SongData songDatas;

                @Override
                public void done(List<ParseObject> objects,
                        com.parse.ParseException e) {
                    if (e == null) {
                        Log.d("Parse", "Objektliste empfangen!");
                        list = (ArrayList<ParseObject>) objects;
                        int j = 0;
                        for (int i = 0; i < list.size(); i++) {
                            String artist = list.get(i).getString("artist");
                            String title = list.get(i).getString("title");
                            String duration = list.get(i).getString("duration");
                            String imageUrl = list.get(i).getString("imageUrl");
                            String anzahlObjekte = String.valueOf(j);
                            Log.d("Parse", "Artist: " + artist);
                            Log.d("Parse", "Title: " + title);
                            Log.d("Parse", "Duration: " + duration);
                            Log.d("Parse", "URL: " + imageUrl);
                            Log.d("Parse", "AnzahlObjekte: " + anzahlObjekte);
                            j++;

                            // /////////////////////////////////////////////////////////////////////////////////////////////
                            // /String in parse.com darf nicht l√§nger als 120
                            // Zeichen sein, sonst NullPointerException!!!//
                            // ////////////////////////////////////////////////////////////////////////////////////////////

                            songDatas = new SongData(title, artist, duration,
                                    imageUrl);
                            details.add(songDatas);

                        }

                    } else {
                        Log.d("Parse", "Objektliste empfangen gescheitert!");

                    }
                    adapter = new SongDataAdapter(details, Liedwahl_Git.this);
                    songListView = (ListView)findViewById(R.id.listeCheck);
                    songListView.setAdapter(adapter);

                    songListView.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {

                            Log.d("Parse", "Item"+position+"geklickt");

                            songListView.setItemChecked(position, true);

                            if (ableToPlay[position]==true) {
                                ableToPlay[position]=false;
                                Log.d("Parse", position + "unchecked");
                            }else {
                                ableToPlay[position]=true;
                                Log.d("Parse", position + "checked");
                            }



                        }

                    });

                }


            });

            return 1;
        }


        protected void onPostExecute(Integer result){
            ok.setVisibility(View.VISIBLE);
            setupOnClickListeners();
            dialog.dismiss();

        }




    }


}

here my activity which tries to receive that array:

package de.lichtenberger.gottschalk.android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseQuery;



public class Lagerfeuer_Lieder extends Activity {

    private ImageButton navLyrics;
    private ImageButton navTune;
    private ImageButton navMusic;
    private ImageButton navChords;
    private ImageView guitar;
    private Button test;
    private String sessionName;
    private ParseObject songList;
    private String ableToPlay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lagerfeuer__lieder);

        Parse.initialize(this, "hGxasGU6e0WQAOh5JIOGDfvFBKrYyBJKXIzxBfAG", "WsOPsXerpsFjsjekKKbZnnjAHvXy5PQHVQEB8Cqu");


        setupUI();
        setupOnClickListeners();

        getSongList();

    }

    private void getSongList() {
        sessionName = getIntent().getExtras().getString("ParseOb");

        ParseQuery pq = new ParseQuery("Sessions");
        pq.whereEqualTo("Name", sessionName);

        pq.getFirstInBackground(new GetCallback() {

            @Override
            public void done(ParseObject object, com.parse.ParseException e) {

                songList = object;
                ableToPlay = songList.getString("AbleToPlay");
            }
        });

        Log.d("Parse", songList.get("AbleToPlay").toString());



    }

    private void setupOnClickListeners() {

        navLyrics.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent navToLyrics = new Intent(Lagerfeuer_Lieder.this, Lyrics.class);
                navToLyrics.putExtra("BackButton", R.string.main);
                startActivity(navToLyrics);



            }
        });

        navTune.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent navToTune = new Intent(Lagerfeuer_Lieder.this, Tune.class);
                startActivity(navToTune);



            }
        });

        navMusic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent navToMusic = new Intent(Lagerfeuer_Lieder.this, Music.class);
                startActivity(navToMusic);



            }
        });

        navChords.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent navToChords = new Intent(Lagerfeuer_Lieder.this, Chords.class);
                startActivity(navToChords);



            }
        });



    }

    private void setupUI() {

        navLyrics = (ImageButton)findViewById(R.id.navlyrics);
        navTune = (ImageButton)findViewById(R.id.navtune);
        navMusic = (ImageButton)findViewById(R.id.navmusic);
        navChords = (ImageButton)findViewById(R.id.navchords);
        guitar = (ImageView)findViewById(R.id.guitar);

        Animation animation = new TranslateAnimation(0, 500,0, 0);
        animation.setDuration(1000);
        animation.setFillAfter(true);
        guitar.startAnimation(animation);
        guitar.setVisibility(0);
        Log.d("Parse", getIntent().getExtras().getString("ParseOb"));


    }

    private void animation() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        String objID = getIntent().getExtras().getString("ParseOb");
        Log.d("Parse", objID);
        ParseQuery pq = new ParseQuery("Sessions");
        pq.getInBackground(objID, new GetCallback() {

            @Override
            public void done(ParseObject arg0, com.parse.ParseException arg1) {
                ParseObject toDest = arg0;
                try {
                    toDest.delete();
                } catch (com.parse.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            });

    }

}

Here you can see that eclipse actually got the Object with the array values:

Eclipse

Hope anyone got an idea how to fix this. Thanks in forward!

share|improve this question
In an if....else block, check if songList.get("AbleToPlay").toString() is not null and use the Log.d("Parse", songList.get("AbleToPlay").toString()) only if the value is not null. Perhaps, there is no value for it. Without seeing the actual data source, I can only speculate. Try and see if the suggestion fixes the crash. – Siddharth Lele Mar 6 at 9:47

1 Answer

Problem is in below code

pq.getFirstInBackground(new GetCallback() {

            @Override
            public void done(ParseObject object, com.parse.ParseException e) {

                songList = object;
                ableToPlay = songList.getString("AbleToPlay");
            }
        });

Please check/debug the method getFirstInBackground which calls the interface GatcallBack with done method. so if that is returning ParseObject as null then there are more chance of getting NullPointerException.

Debugging here will help you lot

share|improve this answer
First thing please remove the Log statement and check that it is crashing or not – Dinesh Prajapati Mar 6 at 9:51
That's true, but I don't get why it is null. Without Log it isn't crashing. But with the ParseObject being null I can not receive the array I need. From my perspective my query should be correct. The tablename is right, and whereEqualTo("Name", sessionName) should retreive the right object. there is no way that sessionName could be wrong writen or something because it is taken from Intent Extra which is put from the same source as the database entry. – Paul Lich Mar 6 at 9:52
okay. please check which object is null. callback method gives null. – Dinesh Prajapati Mar 6 at 9:55
where is parseQuery class? – Dinesh Prajapati Mar 6 at 9:56
ParseQuery is culprit for the problem or null – Dinesh Prajapati Mar 6 at 9:56
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.