0

I am trying to extract the value of some particular string values in another class converter.java class from MainActivity.java , but it's cannot be shown. No Logs are shown. Even, if I want to get Data from another class spinnerSelects.java It's also no longer shown in some places. Can You please help me?

MainActivity.java code is here:

package com.gazzali.spinitmeow;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, View.OnClickListener{

    Spinner spinnerMainChoice;
    Spinner spinnerInputChoice;
    Spinner spinnerOutputChoice;

    EditText getInputValueID;
    Double inputValue;

    TextView outputValueTextViewFromConverter;

    Button buttonConvert;

    String selectedMainChoice;



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

        /* ------------ Main code Starts Here ----------------*/

        /* Main conversion Type choice with Spinner (Drop Down menu)*/
        spinnerMainChoice = findViewById(R.id.spinnerIDMainChoice);
        // [IMPORTANT] Set Spinner Click Listener
        spinnerMainChoice.setOnItemSelectedListener(this);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapterMainChoice = ArrayAdapter.createFromResource(this,
                R.array.MainChoices_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapterMainChoice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinnerMainChoice.setAdapter(adapterMainChoice);

        /* Input Conversion type choice with Spinner */
        spinnerInputChoice = findViewById(R.id.spinnerIDInputChoice);

        /* Output Conversion type choice with Spinner */

        spinnerOutputChoice = findViewById(R.id.spinnerIDOutputChoice);

        /* for input and output fields */
        getInputValueID = findViewById(R.id.editTextIDInputValue);
        /* ---- Setting Button Properties -----*/
        buttonConvert = findViewById(R.id.buttonIDConvert);
        buttonConvert.setOnClickListener(this);

        /* --- Setting Output TextView field ----*/
        outputValueTextViewFromConverter = findViewById(R.id.textViewIDoutputValueToConverter);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // An item was selected. retrieve the selected item
        selectedMainChoice = parent.getSelectedItem().toString();
        Log.i("Selected", selectedMainChoice);
        /* Toast.makeText(MainActivity.this, String.valueOf(inputValue), Toast.LENGTH_SHORT).show();*/

            /* Implement object of spinnerSelects class*/
            spinnerSelects spinnerSelectsInMain = new spinnerSelects(this, spinnerInputChoice, spinnerOutputChoice);
            /* the main EVIL '(context) this' in  the 2nd parameter, 5 hours wasted, but I learnt many more */
            spinnerSelectsInMain.setInputOutputSpinners(selectedMainChoice);

            /* calling test for converter class */
        /*testOnConverter();*/

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

    public void testOnConverter(){
        converter converterInMain = new converter(selectedMainChoice);
    }

    @Override
    public void onClick(View view)
    {
        String inputValueString = getInputValueID.getText().toString();
        inputValue = Double.parseDouble(inputValueString);

        /*Toast.makeText(this, String.valueOf(inputValue), Toast.LENGTH_SHORT).show();*/
        converter converterInMain = new converter(selectedMainChoice);
        double convertedValue = converterInMain.convert(inputValue);
        outputValueTextViewFromConverter.setText(String.valueOf(convertedValue));
    }
}

converter.java codes here:

package com.gazzali.spinitmeow;

import android.content.Context;
import android.util.Log;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

public class converter {


    public String MainChoice, inputChoice, outputChoice;


    public converter() {
    }

    public converter(String selectedMainChoiceFromMain) {
        this.MainChoice = selectedMainChoiceFromMain;

        /* No Log for Main Choice is Being Shown Here */

        Log.i("Main Choice is", MainChoice);
    }

    public converter(String inputChoiceFromSpinnerSelects, String outputChoiceFromSpinnerSelects){
        this.inputChoice = inputChoiceFromSpinnerSelects;
        this.outputChoice = outputChoiceFromSpinnerSelects;

        /* This Logs Shows But ONLY here */
        /* IF I try to show the logs anywhere else in the class, */
        /* Either null exception or No Log output */

        Log.i("Sub Input Choices are:", inputChoice);
        Log.i("Sub Output Choices are:", outputChoice);
    }


    public double convert(double inputValueForEditTextFieldFromMain)
    {
        double inputValueInConverter = inputValueForEditTextFieldFromMain;
        double outputValueToConverterToMain= 0.00;

        /* Here I can't see the Log */
        /* Apps Crashes When I press Convert Button*/
         Log.i("Sub Input Choices are:", inputChoice);

        converterLength converterLengthInConverter = new converterLength();

        outputValueToConverterToMain = inputValueInConverter * 22;


        /*switch (MainChoice)
        {
            case "Length":
                outputValueToConverterToMain = converterLengthInConverter.convertLength(inputChoice, outputChoice, inputValueInConverter);
            break;
        }*/
        return outputValueToConverterToMain;
    }
}

Apps Keeep crashing when I press the convert button.

Locat error shows :

2019-07-21 16:48:33.406 10979-10979/com.gazzali.spinitmeow E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gazzali.spinitmeow, PID: 10979
java.lang.NullPointerException: println needs a message
    at android.util.Log.println_native(Native Method)
    at android.util.Log.i(Log.java:166)
    at com.gazzali.spinitmeow.converter.convert(converter.java:46)
    at com.gazzali.spinitmeow.MainActivity.onClick(MainActivity.java:104)

UPDATE here's my spinnerSelects class which passes inputChoice and outputChoice parameter to converter class, but still I am geing that weird error. Although I set the constructor values of converter class by the parameter passed by spinnerSelects , it never sets it values as I can't see inputChoice value inside converter class

package com.gazzali.spinitmeow;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class spinnerSelects implements AdapterView.OnItemSelectedListener{

    public String inputChoice, outputChoice;
    public Spinner spinnerInputChoice, spinnerOutputChoice;
    public ArrayAdapter<CharSequence> adapterInputChoice, adapterOutputChoice;

    private Context contextInSpinnerSelects;

    public Context getContextInSpinnerSelects() {
        return contextInSpinnerSelects;
    }

    public spinnerSelects() {
        /* Empty Constructor */
    }

    public spinnerSelects(Context contextFromMain, Spinner spinnerInputChoiceFromMain, Spinner spinnerOutputChoiceFromMain) {
        this.spinnerInputChoice = spinnerInputChoiceFromMain;
        this.spinnerOutputChoice = spinnerOutputChoiceFromMain;
        this.contextInSpinnerSelects = contextFromMain;
    }

    /**
     *
     * @param selectedMainChoice String retrieves Main Conversion spinner's type
     */
    public void setInputOutputSpinners(String selectedMainChoice) {
        switch (selectedMainChoice)
        {
            case "Length": {
                adapterInputChoice = ArrayAdapter.createFromResource(contextInSpinnerSelects,
                        R.array.LengthChoices_array, android.R.layout.simple_spinner_item);

                adapterOutputChoice = ArrayAdapter.createFromResource(contextInSpinnerSelects,
                        R.array.LengthChoices_array, android.R.layout.simple_spinner_item);

                setInputOutputListenerAndDropDownAndAdapter();
            }
            break;

            case "Temperature": {
                adapterInputChoice = ArrayAdapter.createFromResource(contextInSpinnerSelects,
                        R.array.TemperatureChoices_array, android.R.layout.simple_spinner_item);

                adapterOutputChoice = ArrayAdapter.createFromResource(contextInSpinnerSelects,
                        R.array.TemperatureChoices_array, android.R.layout.simple_spinner_item);

                setInputOutputListenerAndDropDownAndAdapter();
            }
            break;

            case "Weight": {
                adapterInputChoice = ArrayAdapter.createFromResource(contextInSpinnerSelects,
                        R.array.WeightChoices_array, android.R.layout.simple_spinner_item);

                adapterOutputChoice = ArrayAdapter.createFromResource(contextInSpinnerSelects,
                        R.array.WeightChoices_array, android.R.layout.simple_spinner_item);

                setInputOutputListenerAndDropDownAndAdapter();

            }
            break;
        }
    }
    private void setInputOutputListenerAndDropDownAndAdapter() {

        spinnerInputChoice.setOnItemSelectedListener(this);
        spinnerOutputChoice.setOnItemSelectedListener(this);

        adapterInputChoice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerInputChoice.setAdapter(adapterInputChoice);
        adapterOutputChoice.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerOutputChoice.setAdapter(adapterOutputChoice);

    }

    public Spinner getSpinnerInputChoice() {
        return spinnerInputChoice;
    }

    public Spinner getSpinnerOutputChoice() {
        return spinnerOutputChoice;
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        inputChoice = spinnerInputChoice.getSelectedItem().toString();
        outputChoice = spinnerOutputChoice.getSelectedItem().toString();

        converter converterInSpinnerSelects = new converter(inputChoice, outputChoice);
        /*converterInSpinnerSelects.setInputOutputChoice(inputChoice, outputChoice);*/
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

1 Answer 1

2

Your problem might be that inputChoice is never set.

You are creating a new instance of your converter-class with this code:

converter converterInMain = new converter(selectedMainChoice);

this will ONLY run this code in your converter-instance:

public converter(String selectedMainChoiceFromMain) {
    this.MainChoice = selectedMainChoiceFromMain;
    /* No Log for Main Choice is Being Shown Here */
    Log.i("Main Choice is", MainChoice);
}

the second constructor therefore never executes public converter(String inputChoiceFromSpinnerSelects, String outputChoiceFromSpinnerSelects){ what means that you never set inputChoicenor outputChoice in your converter-instance which means that they stay null

Log.i("Sub Input Choices are:", inputChoice); therefore gets a null as inputChoice

But I don't think this is the problem here. It should still work but just output a null into your console. As I remember Log.i() works like this: Log.i(TAG, MESSAGE) so your code should look something like this:

Log.i("CONVERTER", "Sub Input Choices are: " + inputChoice);

UPDATE Well now you are creating a new instance of convertor in (AdapterView<?> parent, View view, int position, long id) {

converter converterInSpinnerSelects = new converter(inputChoice, outputChoice);

this will NOT update the values of the convertor-instance you create in your onClick(View view)-function.

One way of solving this problem might be to simply put the Strings static:

public static String MainChoice, inputChoice, outputChoice;

But then there is no need for creating a new instance of converter every time... so simply do this then:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    converter.inputChoice = spinnerInputChoice.getSelectedItem().toString();
    converter.outputChoice = spinnerOutputChoice.getSelectedItem().toString();
}
Sign up to request clarification or add additional context in comments.

5 Comments

I have updated my question, but it still doesn't work. Can you please help?
Wow ! It solves my problem, Thanks a lot. Can you please tell me where the problem were? and also what magic did static did?
Cool, that I could help. Basically your problem was that everytime you call new converter you create a new copy of the converter-class which means that all the Strings are set to null at this point. Even if you change the values in one instance it won't change them in the other instances. This means that one instance does not know what it is saying in another.
static means that this value is only stored one time in the RAM and is for every instance the same. This will affect your code if you create multiple instances of convertor at the same time and having different sets of Strings. Then you would have a concurrency problem and have to manually set the strings for each instance. static basically is like a tunnel through all the instances of your class you created for this one object.
If you think this is it you can upvote my answer and mark it as accepted answer. This would help others searching for the same problem and reaching this thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.