Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That <code>NullPointerException</code> comes from the fact that you initialize your <code>Views</code> using the <code>getActivity()</code> method where you declare them as fields in the <code>F_NPV</code> class. The method <code>getActivity()</code> method will return a valid <code>Activity</code> reference after the callback <code>onAttach()</code> is called, so the way you initialize the views will not work as, at that moment(when the fields of the <code>Fragment</code> class are initialized) the method <code>getActivity</code> will return <code>null</code>, no valid reference. The correct way to do that initialization is doing it in the <code>onActivityCreated</code> callback:</p> <pre><code>EditText r; Button calc; //... @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); r = (EditText) getActivity().findViewById(R.id.npv_rate); calc = (Button) getActivity().findViewById(R.id.npv_calc); //... </code></pre> <p>Also, if I may, some suggestions regarding your code:</p> <p>You're doing some double's parsing from Strings and it may be a good idea to check the input so you don't throw a <code>NumberFormatException</code>. For example, if the user creates some <code>EditTexts</code> and then clicks the calculate <code>Button</code>(I know, it sounds silly, but there are chances the user will do it(I did it for example)), you'll throw a <code>NumberFormatException</code> as you try to parse an empty <code>String</code>. Instead make a little check:</p> <pre><code>public void onClick(View arg0) { Double r1 = Double.parseDouble((r.getText().toString()) .equals("") ? "0" : r.getText().toString()); EditText editText = (EditText) getActivity().findViewById(i); TextView answer = (TextView) getActivity().findViewById(R.id.npv_answer); double[] CashFlows; CashFlows = new double[i]; double result = 0; String tmp = editText.getText().toString(); CashFlows[i] = (Double.parseDouble(tmp.equals("") ? "0" : tmp)) / (Math.pow(1 + r1, i)); //... </code></pre> <p>Also, even if you have correct values in the <code>EditText</code> the above code will throw a <code>NullPointerException</code>, as the <code>editText</code> variable will be <code>null</code>. The reason for this is in the <code>while</code> loops that you used to create the fields. For example, if the user moves the <code>SeekBar</code> to 3 than the <code>while</code> loop will run 3 times, each time incrementing the <code>i</code> value. So <code>i</code> will be <code>0</code>, <code>1</code>, <code>2</code>, so far correct but because you increment <code>i</code> each time the final <code>i</code> will be <code>4</code>. Now in the <code>onClick</code> method you'll look for an <code>EditText</code> with the id <code>i</code>, but as there is no <code>EditText</code> in the layout with the id <code>4</code>, the view will be <code>null</code>. </p> <p>Also, try to give your classes better names, you may know very well what they mean but you could be making things worse for someone that reads your code(like F_PNV, F_PV etc).</p> <p>Code for the <code>onActivityCreated</code> method. This should solve what you're trying to do(if I understand what you want):</p> <pre><code>@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); r = (EditText) getActivity().findViewById(R.id.npv_rate); calc = (Button) getActivity().findViewById(R.id.npv_calc); final LinearLayout linearLayout = (LinearLayout) getActivity() .findViewById(R.id.npv_calcfields); SeekBar bar = (SeekBar) getActivity().findViewById(R.id.npv_seekbar); final TextView selection = (TextView) getActivity().findViewById( R.id.npv_selected); bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) { selection .setText("You have selected " + progress + " periods."); if (progress == 0) { String normalstring = getActivity().getResources() .getString(R.string.npv1); selection.setText(normalstring); linearLayout.removeAllViews(); // the progress is 0 so // remove all the views that // are currently present } else { int currentChilds = linearLayout.getChildCount(); if (currentChilds &lt; progress) { while (currentChilds != progress) { EditText editText = new EditText(getActivity()); editText.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); linearLayout.addView(editText); currentChilds++; } } else if (currentChilds &gt; progress) { while (currentChilds != progress) { linearLayout.removeViewAt(linearLayout .getChildCount() - 1); currentChilds--; } } } } public void onStopTrackingTouch(SeekBar arg0) { } public void onStartTrackingTouch(SeekBar arg0) { } }); calc.setOnClickListener(new OnClickListener() { public void onClick(View view) { Double r1 = Double.parseDouble((r.getText().toString()) .equals("") ? "0" : r.getText().toString()); TextView answer = (TextView) getActivity().findViewById( R.id.npv_answer); final LinearLayout linearLayout = (LinearLayout) getActivity() .findViewById(R.id.npv_calcfields); int size = linearLayout.getChildCount(); double[] CashFlows = new double[size]; double result = 0; for (int i = 0; i &lt; size; i++) { EditText editText = (EditText) linearLayout.getChildAt(i); String tmp = editText.getText().toString(); CashFlows[i] = (Double.parseDouble(tmp.equals("") ? "0" : tmp)) / (Math.pow(1 + r1, i)); } for (double d : CashFlows) { result += d; } answer.setText("answer is " + result); } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload