mardi 21 juillet 2015

android error with format of sql query

I am working with a data base in android and am getting a error, as I am pretty new to this I think from the logcat that it is a format error. While the error is rather lengthy I will include a modified version. The editing was identical errors with different PID's:

07-20 19:24:59.323    8037-8037/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bkane56.practice.practiceapp, PID: 8037
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.bkane56.practice.practiceapp/com.bkane56.practice.practiceapp.LeaveAriveActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
            at android.app.Activity.findViewById(Activity.java:2082)
            at com.bkane56.practice.practiceapp.LeaveAriveActivity.<init>(LeaveAriveActivity.java:25)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1606)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1089)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)


        ***********SEVERAL OF THE ABOVE EXCEPTIONS*******************


07-20 19:43:13.998  11220-11220/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bkane56.practice.practiceapp, PID: 11220
    java.lang.IllegalStateException: Could not find a method getDepartureTime()(View) in the activity class com.bkane56.practice.practiceapp.LeaveAriveActivity for onClick handler on view class android.widget.Button with id 'button'
            at android.view.View$1.onClick(View.java:4012)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.NoSuchMethodException: getDepartureTime() [class android.view.View]
            at java.lang.Class.getMethod(Class.java:664)
            at java.lang.Class.getMethod(Class.java:643)
            at android.view.View$1.onClick(View.java:4005)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)

        ***********SEVERAL OF THE ABOVE EXCEPTIONS*******************

07-20 19:45:27.815  12549-12549/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bkane56.practice.practiceapp, PID: 12549
    java.lang.IllegalStateException: Could not find a method getDepartureTime()(View) in the activity class com.bkane56.practice.practiceapp.LeaveAriveActivity for onClick handler on view class android.widget.Button with id 'button'
            at android.view.View$1.onClick(View.java:4012)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.NoSuchMethodException: getDepartureTime() [class android.view.View]
            at java.lang.Class.getMethod(Class.java:664)
            at java.lang.Class.getMethod(Class.java:643)
            at android.view.View$1.onClick(View.java:4005)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

07-21 09:44:21.886  11608-11608/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bkane56.practice.practiceapp, PID: 11608
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4025)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4020)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.util.UnknownFormatConversionException: Conversion: "
            at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1399)
            at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1336)
            at java.util.Formatter.transform(Formatter.java:1442)
            at java.util.Formatter.doFormat(Formatter.java:1081)
            at java.util.Formatter.format(Formatter.java:1042)
            at java.util.Formatter.format(Formatter.java:1011)
            at java.lang.String.format(String.java:1803)
            at java.lang.String.format(String.java:1777)
            at com.bkane56.practice.practiceapp.LeaveAriveActivity.getQuery(LeaveAriveActivity.java:178)
            at com.bkane56.practice.practiceapp.LeaveAriveActivity.getDepartureTime(LeaveAriveActivity.java:157)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4020)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19884)
            at android.os.Handler.handleCallback(Handler.java:746)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

        ***********SEVERAL OF THE ABOVE EXCEPTIONS*******************

    --------- beginning of system

This is the activity which throws the error:

package com.bkane56.practice.practiceapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;


public class LeaveAriveActivity extends Activity {

    private MetroLinkDatabaseHelper myHelper =
            MetroLinkDatabaseHelper.getInstance(this);

    private Spinner spinArrive;
    private Spinner spinLeave;
    private Spinner spinDayOfWeek;
    private Spinner spinHour;
    private Spinner spinMinute;
    private Spinner spinAmPm;

    private String leaveStopName;
    private String arriveStopName;
    private String arriveTime;
    private String dayOfWeek;
    private String arrivalColor;
    private String departureColor;
    private String tripDirection = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_arive);

        populateSpinners();
    }


    private void populateSpinners() {

        spinArrive = (Spinner) findViewById(R.id.spinArriveMetroStopList);
        spinLeave = (Spinner) findViewById(R.id.spinDepartMetroStopList);
        spinDayOfWeek = (Spinner) findViewById(R.id.spinDayOfWeek);
        spinHour = (Spinner) findViewById(R.id.spinHour);
        spinMinute = (Spinner) findViewById(R.id.spinMinute);
        spinAmPm = (Spinner) findViewById(R.id.spinAmPm);

//        Get the list of metrolink stop names
        List<String> metroStop = myHelper.getAllStops();

//        populate the arrival and departure spinners from DB
        ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, R.layout.simplerow, metroStop);

        spinArrive.setAdapter(listAdapter);
        spinLeave.setAdapter(listAdapter);

//        poulate day of the week spinner from value list of days of week
        ArrayAdapter<CharSequence> dayAdapter = ArrayAdapter.createFromResource(this,
                R.array.day_of_week, R.layout.clock_layout);
        spinDayOfWeek.setAdapter(dayAdapter);

//        pouulate minutes spinner from value list of minutes

        ArrayAdapter<CharSequence> hrAdapter = ArrayAdapter.createFromResource(this,
                R.array.hours, R.layout.clock_layout);
        spinHour.setAdapter(hrAdapter);

//        pouulate minutes spinner from value list of minutes
        ArrayAdapter<CharSequence> minAdapter = ArrayAdapter.createFromResource(this,
                R.array.minutes, R.layout.clock_layout);
        spinMinute.setAdapter(minAdapter);

//        pouulate am/pm spinner from value list of am/pm
        ArrayAdapter<CharSequence> ampmAdapter = ArrayAdapter.createFromResource(this,
                R.array.an_pm, R.layout.clock_layout);
        spinAmPm.setAdapter(ampmAdapter);
    }

    private void setQueryInfo() {

//        gets time from spinner properly formattec for the query/ DB
        arriveTime = setTime();
        arriveStopName = spinArrive.getSelectedItem().toString();
        leaveStopName = spinLeave.getSelectedItem().toString();

        boolean arMidStop = (!Arrays.asList(RED_STOPS).contains(arriveStopName)) &&
                (!Arrays.asList(BLUE_STOPS).contains(arriveStopName));
        boolean deptMidStop = (!Arrays.asList(RED_STOPS).contains(leaveStopName)) &&
                (!Arrays.asList(BLUE_STOPS).contains(leaveStopName));

        if(Arrays.asList(BLUE_STOPS).contains(arriveStopName)) {
            arrivalColor = "B";
        }else if(Arrays.asList(RED_STOPS).contains(arriveStopName)) {
            arrivalColor = "R";
        }//        if the arrival is in the middle and you
//          departure was from a blue stop set arrivalcolor = "B"
        else if(arMidStop && Arrays.asList(BLUE_STOPS).contains(leaveStopName)) {
            arrivalColor = "B";
        }//          if the arrival is in the middle and you
//            departure was from a red stop or both stops in the middle set arrivalcolor = "R"
        else {
            arrivalColor = "R";
        }

//        same as setting arrival but instead for departure colors
        if(Arrays.asList(BLUE_STOPS).contains(leaveStopName)) {
            departureColor = "B";
        }else if((Arrays.asList(RED_STOPS).contains(leaveStopName))) {
            departureColor = "R";
        }else if(deptMidStop && Arrays.asList(BLUE_STOPS).contains(arriveStopName)) {
            departureColor = "B";
        }else {
            departureColor = "R";
        }
//        Set the day of the week.
//        If weekday set it to monday as all weekdays schedules are the same
        dayOfWeek = spinDayOfWeek.getSelectedItem().toString().toLowerCase();

        if(dayOfWeek.equals("week day")) {
            dayOfWeek = "monday";
        }
//
    }

    private String setTime () {

        String hour ;
        String minute;
        String amPm;

        String  strHour = spinHour.getSelectedItem().toString();
        int intHour = Integer.parseInt(strHour);

        minute = spinMinute.getSelectedItem().toString();
        amPm = spinAmPm.getSelectedItem().toString();
        if (amPm.equals("PM")) {
            hour = String.valueOf(intHour + 12);
        }else {
            hour = strHour;
        }
        String time = String.format("%s:%s:00", hour, minute);

        return time;
    }

    public void getDepartureTime(View view) {

        setQueryInfo();
        String query = getQuery();
        String departTime = myHelper.getDepartureTime(query);

        TextView myDepart = (TextView) findViewById(R.id.tvDepartTime);
        TextView myDepartText = (TextView) findViewById(R.id.tvLeaveStation);
        TextView at = (TextView) findViewById(R.id.tvAt);

        at.setVisibility(View.VISIBLE);
        myDepart.setVisibility(View.VISIBLE);
        myDepart.setText(departTime);
        myDepartText.setText("Your Train Leaves:");

    }

    private String getQuery() {

        String query;

// "THIS IS LINE 178"   

        query = String.format("SELECT st.departure_time AS _id FROM routes r \n" +
                "JOIN trips t on t.route_id = r.route_id \n" +
                "JOIN calendar c on c.service_id =t.service_id \n" +
                "JOIN stop_times st on st.trip_id = t.trip_id \n" +
                "JOIN stops s on s.stop_id = st.stop_id\n" +
                "\n" +
                "Where s.stop_name LIKE \"%s%\"\n" +
                "\n" +
                "\n" +
                "AND st.trip_id = \n" +
                "\t\n" +
                "--This gets the trip_id of the time and station selected\n" +
                "(SELECT t.trip_id FROM routes r \n" +
                "\t\t\n" +
                "\tJOIN trips t on t.route_id = r.route_id \n" +
                "\tJOIN calendar c on c.service_id =t.service_id \n" +
                "\tJOIN stop_times st on st.trip_id = t.trip_id \n" +
                "\tJOIN stops s on s.stop_id = st.stop_id\n" +
                "\t\t\n" +
                "\tWHERE r.route_id LIKE \"12594%s\" \n" +
                "\t\tAND s.stop_name LIKE \"%s\" \n" +
                "\t\tAND t.direction_id = \"%s\" \n" +
                "\t\tAND st.arrival_time >= \"%s\" \n" +
                "\t\tAND c.%s = \"1\"\n" +
                "\t\tORDER BY st.departure_time\n" +
                "\t\tLIMIT 1)\n" +
                ";\n", leaveStopName, arrivalColor, arriveStopName ,tripDirection, arriveTime, dayOfWeek);
        return query;
    }

    private boolean getTransfer() {

        if(arrivalColor.equals(departureColor)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_leave_arive, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private final String[]  RED_STOPS = {
            "LAMBERT MAIN TRML METROLINK STATION",
            "LAMBERT EAST TRML METROLINK STATION",
            "NORTH HANLEY METROLINK STATION",
            "UMSL NORTH METROLINK STATION",
            "ROCK ROAD METROLINK STATION",
            "WELLSTON METROLINK STATION",
            "DELMAR METROLINK STATION",
            "MEMORIAL HOSPITAL METROLINK STATION",
            "SWANSEA METROLINK STATION",
            "BELLEVILLE METROLINK STATION",
            "COLLEGE METROLINK STATION",
            "SHILOH-SCOTT METROLINK STATION"};

    private final String[] BLUE_STOPS = {
            "RICHMOND HEIGHTS METROLINK STATION",
            "CLAYTON METROLINK STATION",
            "BRENTWOOD METROLINK STATION",
            "MAPLEWOOD METROLINK STATION",
            "SUNNEN METROLINK STATION",
            "SHREWSBURY METROLINK STATION",
            "FORSYTH METROLINK STATION",
            "UMSL SOUTH METROLINK STATION",
            "SKINKER METROLINK STATION"};
}

The called method in myHelper looks like this, although it appears to crash before reaching this method.

        public String getDepartureTime(String sqlQueryDepart) {
        this.sqlQueryDepart =sqlQueryDepart;

        String departTime = null;
        SimpleDateFormat fromDB = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat myTime = new SimpleDateFormat("h:mm a");

//        open database
        open();

        Cursor c = myDataBase.rawQuery(sqlQueryDepart, null);

        if (c.moveToFirst()) {

            try {
                departTime = myTime.format(fromDB.parse(c.getString(0)));

            } catch (ParseException e) {

            }

            if (!c.isClosed()) {
                c.close();
            }

        }
//        close database
        close();
        return departTime;
    }

I cannot figure out what the hell is going on.

Any help would be greatly apprecieated.

Aucun commentaire:

Enregistrer un commentaire