Android Java FATAL EXCEPTION java.lang.NullPointerException

I am new to Android, I know a little JAVA, but I want to study, and I continue to study textbooks. The idea of ​​what I want to do is as follows: I have a menu, for example: 1. BIRDS 2. ROCKY 3. INSTALLATIONS and when I press BIRDS, I want to show photos and a little description. Image id and description are stored in xml. Like this:

<signs> <sign id="1_1" category="1"> <name>desc1</name> </sign> <sign id="1_2" category="1"> <name>desc2</name> </sign> <sign id="1_3_1" category="1"> <name>desc3</name> </sign> <sign id="1_3_2" category="1"> <name>desc4</name> </sign> </signs> 

The image goes as sign_1_1.png, sign_1_2.png in drawables.

I made a gallery, it shows, description and images are also visible. I did this in the image gallery of the Selected Image to display the corresponding description in the TextView. But when I click, I get FATAL EXCEPTION:

 E/AndroidRuntime(22141): FATAL EXCEPTION: main E/AndroidRuntime(22141): java.lang.NullPointerException E/AndroidRuntime(22141): at apcmag.examples.singleSignListItem$ImageAdapter.getView(singleSignListItem.java:117) E/AndroidRuntime(22141): at android.widget.Gallery.makeAndAddView(Gallery.java:849) E/AndroidRuntime(22141): at android.widget.Gallery.fillToGalleryRightLtr(Gallery.java:803) E/AndroidRuntime(22141): at android.widget.Gallery.fillToGalleryRight(Gallery.java:747) E/AndroidRuntime(22141): at android.widget.Gallery.layout(Gallery.java:656) E/AndroidRuntime(22141): at android.widget.Gallery.onLayout(Gallery.java:351) E/AndroidRuntime(22141): at android.view.View.layout(View.java:13754) E/AndroidRuntime(22141): at android.view.ViewGroup.layout(ViewGroup.java:4362) 

Code:

 package apcmag.examples; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.TypedArray; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class singleSignListItem extends Activity { public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); this.setContentView(R.layout.single_sign_gallery); Gallery g = (Gallery) findViewById(R.id.gallery); final Intent i = getIntent(); final String REGEX = "/%%/"; String product = i.getStringExtra("product"); setTitle(product); g.setAdapter(new ImageAdapter(this)); g.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String [] products = i.getStringExtra("product_text").split(REGEX); Toast.makeText(singleSignListItem.this, ""+position, Toast.LENGTH_SHORT).show(); TextView show_intro = (TextView) findViewById(R.id.show_intro); show_intro.setText(""+products[position]); } public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; // private Integer[] mImageIds = { // R.drawable.sign_1_1, // R.drawable.sign_1_1, // R.drawable.sign_1_1, // R.drawable.sign_1_1 // }; private Integer[] mImages = takePhotos(); public Integer[] takePhotos (){ Intent g = getIntent(); String Reg = "/%%/"; String Reg2 = "_%_"; String dataList = g.getStringExtra("product_text"); String [] datastring = dataList.split(Reg); Integer[] imageResource = new Integer[20]; String[] dd = null; for(int k = 0; k<datastring.length;k++){ dd = datastring[k].split(Reg2); String imagename = "sign_"+dd[0]; imageResource[k] = getResources().getIdentifier(imagename, "drawable", getPackageName()); } return imageResource; } public ImageAdapter(Context c) { mContext = c; TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); mGalleryItemBackground = a.getResourceId( R.styleable.HelloGallery_android_galleryItemBackground, 0); a.recycle(); } public int getCount() { return mImages.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(mContext); i.setImageResource(mImages[position]); i.setLayoutParams(new Gallery.LayoutParams(115, 200)); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setBackgroundResource(mGalleryItemBackground); return i; } } } 

I cannot understand what the problem is for several hours, and I have some ideas that may be here:

  i.setLayoutParams(new Gallery.LayoutParams(115, 200)); 

but i'm not sure what to do

UPDATE

I actually found the problem:

On line 77, I initialized an ImageResource of size 20

 Integer[] imageResource = new Integer[20]; 

but by date there were only 4 items

 String [] datastring = dataList.split(Reg); 

therefore the variable mImages

  private Integer[] mImages = takePhotos(); 

will have 20 elements, of which 16 will be zero and at the end

 i.setImageResource(mImages[position]); 

it could not display null elements and crashed.

So I have another question:

If I do not know the size of a valid integer [], how can I initialize and insert elements into it? With a list?

+4
source share
4 answers

I actually found the problem:

On line 77, I initialized an ImageResource of size 20

Integer [] imageResource = new Integer [20];

but by date there were only 4 items

 String [] datastring = dataList.split(Reg); 

therefore the variable mImages

  private Integer[] mImages = takePhotos(); 

will have 20 elements, of which 16 will be zero, and at the end

 i.setImageResource(mImages[position]); 

he could not display the null elements and crashed.

+1
source

public Object getItem (int position) {return position; } public long getItemId (int position) {return position; } check return type. I think both will be int.

0
source

There is an error in this line:

 for(int k = 0; k<datastring.length;k++){ dd = datastring[k].split(Reg2); 

I think this will do the work for you:

 dd = datastring.split(Reg2); 
-1
source

I think the problem is in String [] dd = null; code. u should initialize the dd variable

-2
source

Source: https://habr.com/ru/post/1439421/


All Articles