Click here to Skip to main content
15,889,116 members
Home / Discussions / C#
   

C#

 
GeneralRe: Determining if a time zone is currently in Daylight Saving Time Pin
OriginalGriff9-Feb-17 23:29
mveOriginalGriff9-Feb-17 23:29 
GeneralRe: Determining if a time zone is currently in Daylight Saving Time Pin
TNCaver10-Feb-17 9:04
TNCaver10-Feb-17 9:04 
GeneralRe: Determining if a time zone is currently in Daylight Saving Time Pin
Richard Deeming10-Feb-17 9:24
mveRichard Deeming10-Feb-17 9:24 
AnswerRe: Determining if a time zone is currently in Daylight Saving Time Pin
Gerry Schmitz9-Feb-17 22:22
mveGerry Schmitz9-Feb-17 22:22 
GeneralRe: Determining if a time zone is currently in Daylight Saving Time Pin
TNCaver10-Feb-17 9:26
TNCaver10-Feb-17 9:26 
AnswerRe: Determining if a time zone is currently in Daylight Saving Time Pin
Richard Deeming10-Feb-17 2:34
mveRichard Deeming10-Feb-17 2:34 
GeneralRe: Determining if a time zone is currently in Daylight Saving Time Pin
TNCaver10-Feb-17 9:24
TNCaver10-Feb-17 9:24 
QuestionAndroid Day of Week Calculator Pin
Pavlex49-Feb-17 9:14
Pavlex49-Feb-17 9:14 
I have created program that calculates day of week when entered date in format "January 7 2000" .Why do I get NullPointerException exception when I click button to calculate day of week?

Here is source code of app:

Java
public class MainActivity extends AppCompatActivity
{
    private int position;
    private int value;
    private Button button;
    private EditText editText;
    private TextView textView1,textView2;
    private Spinner spinner,spinner2;
    private ArrayAdapter<CharSequence> adapter;

    private boolean isValid;
    private Date date;
    private int inMonth, inDay, inYear;

    static boolean isDateValid(int month, int day, int year)
    {
        boolean validation = true;
        int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        if (month < 1 || month > 12)
        {
            validation = false;
        }

        if (day < 1 || day > daysInMonth[month - 1])
        {
            validation = false;
        }

        if (year < 1700 || year > 3000)
        {
            validation = false;
        }
        return validation;
    }

    static String zellerCalc(int month, int day, int year)
    {
        String dayOfWeek;
        int m = -1; 
        int h = -1; 
        int q = day;
        int k;
        int j;

        if (month == 1)
        {
            m = 13;
        }
        else if (month == 2)
        {
            m = 14;
        }
        else
        {
            m = month;
        }

        if (m == 13 || m == 14)
        {
            year--;
        }

        k = year % 100;
        j = year / 100; 

        h = (q + (int)((13 * (m + 1)) / 5.0) + k + (int)(k / 4.0) + (int)(j / 4.0) + (5 * j)) % 7;

        if (h == 0)
        {
            dayOfWeek = "Subota";
        }
        else if (h == 1)
        {
            dayOfWeek = "Nedelja";
        }
        else if (h == 2)
        {
            dayOfWeek = "Ponedeljak";
        }
        else if (h == 3)
        {
            dayOfWeek = "Utorak";
        }
        else if (h == 4)
        {
            dayOfWeek = "Sreda";
        }
        else if (h == 5)
        {
            dayOfWeek = "Četvrtak";
        }
        else
            dayOfWeek = "Petak";

        return dayOfWeek;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        button = (Button) findViewById(R.id.button_calculate);
        textView1 = (TextView) findViewById(R.id.textView_day);
        textView2 = (TextView) findViewById(R.id.textView_year);
        spinner = (Spinner) findViewById(R.id.spinner_month);
        spinner2 = (Spinner) findViewById(R.id.spinner_day);
        editText = (EditText) findViewById(R.id.editText_year);

        adapter = ArrayAdapter.createFromResource(this,R.array.months,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        adapter = ArrayAdapter.createFromResource(this,R.array.day,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter);

        if (spinner2.getSelectedItemPosition() == 0)
            value = 1;
        if (spinner2.getSelectedItemPosition() == 1)
            value = 2;
        if (spinner2.getSelectedItemPosition() == 2)
            value = 3;
        if (spinner2.getSelectedItemPosition() == 3)
            value = 4;
        if (spinner2.getSelectedItemPosition() == 4)
            value = 5;
        if (spinner2.getSelectedItemPosition() == 5)
            value = 6;
        if (spinner2.getSelectedItemPosition() == 6)
            value = 7;
        if (spinner2.getSelectedItemPosition() == 7)
            value = 8;
        if (spinner2.getSelectedItemPosition() == 8)
            value = 9;
        if (spinner2.getSelectedItemPosition() == 9)
            value = 10;
        if (spinner2.getSelectedItemPosition() == 10)
            value = 11;
        if (spinner2.getSelectedItemPosition() == 11)
            value = 12;

        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                String dayOfWeek;
                boolean isValid;

                Calendar c = Calendar.getInstance();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                String strDate = sdf.format(c.getTime());
            do
            {
                try
                {
                    String getdate = spinner.getItemAtPosition(position).toString() + value
                            + textView2.getText().toString();
                    Date date = sdf.parse(getdate);
                }
                catch (ParseException ex)
                {

                }

                isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear());

                if (isValid == false)
                {
                   Toast.makeText(MainActivity.this,"You entered wrong date",Toast.LENGTH_SHORT).show();
                }
            }
            while (isValid == false);

                dayOfWeek = zellerCalc(date.getMonth(), date.getDay(), date.getYear());

                textView1.setText(dayOfWeek);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();

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

        return super.onOptionsItemSelected(item);
    }
}


Logcat:

02-07 12:32:16.349 3049-3049/com.example.pavle.dayofborn E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pavle.dayofborn, PID: 3049 java.lang.NullPointerException at com.example.pavle.dayofborn.MainActivity$2.onClick(MainActivity.java:230) at android.view.View.performClick(View.java:4633) at android.view.View$PerformClick.run(View.java:19270) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5476) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)

//Line 230: isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear());
AnswerRe: Android Day of Week Calculator Pin
NotPolitcallyCorrect9-Feb-17 9:47
NotPolitcallyCorrect9-Feb-17 9:47 
AnswerRe: Android Day of Week Calculator Pin
Eddy Vluggen9-Feb-17 10:50
professionalEddy Vluggen9-Feb-17 10:50 
AnswerRe: Android Day of Week Calculator Pin
Richard MacCutchan9-Feb-17 21:02
mveRichard MacCutchan9-Feb-17 21:02 
AnswerRe: Android Day of Week Calculator Pin
Gerry Schmitz10-Feb-17 12:55
mveGerry Schmitz10-Feb-17 12:55 
QuestionChange the height PopupMenu ? Pin
Member 24584678-Feb-17 17:58
Member 24584678-Feb-17 17:58 
AnswerRe: Change the height PopupMenu ? Pin
Mycroft Holmes8-Feb-17 20:15
professionalMycroft Holmes8-Feb-17 20:15 
QuestionWCF Transport Security And SSL Pin
Liagapi8-Feb-17 17:34
Liagapi8-Feb-17 17:34 
AnswerRe: WCF Transport Security And SSL Pin
Richard Deeming8-Feb-17 22:06
mveRichard Deeming8-Feb-17 22:06 
QuestionC# : In a Powerpoint slide, Unable to set Play Full Screen / Start Automatically of a Video (Shape) object Pin
svpmirashi8-Feb-17 1:51
svpmirashi8-Feb-17 1:51 
AnswerRe: C# : In a Powerpoint slide, Unable to set Play Full Screen / Start Automatically of a Video (Shape) object Pin
Gerry Schmitz9-Feb-17 7:53
mveGerry Schmitz9-Feb-17 7:53 
GeneralRe: C# : In a Powerpoint slide, Unable to set Play Full Screen / Start Automatically of a Video (Shape) object Pin
svpmirashi9-Feb-17 20:48
svpmirashi9-Feb-17 20:48 
GeneralRe: C# : In a Powerpoint slide, Unable to set Play Full Screen / Start Automatically of a Video (Shape) object Pin
Gerry Schmitz9-Feb-17 21:17
mveGerry Schmitz9-Feb-17 21:17 
GeneralRe: C# : In a Powerpoint slide, Unable to set Play Full Screen / Start Automatically of a Video (Shape) object Pin
svpmirashi9-Feb-17 20:50
svpmirashi9-Feb-17 20:50 
GeneralRe: C# : In a Powerpoint slide, Unable to set Play Full Screen / Start Automatically of a Video (Shape) object Pin
Gerry Schmitz9-Feb-17 21:20
mveGerry Schmitz9-Feb-17 21:20 
Question(C#) How to make this kind of design and functions Pin
JeezyWonder7-Feb-17 19:27
JeezyWonder7-Feb-17 19:27 
AnswerRe: (C#) How to make this kind of design and functions Pin
Richard MacCutchan7-Feb-17 21:44
mveRichard MacCutchan7-Feb-17 21:44 
QuestionHow do I synchronize multi user GUI controls automatically when changes happen into database from any of the user? Pin
Member 102589347-Feb-17 3:10
Member 102589347-Feb-17 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.