关于Iterator会报空指针的方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ckdroid/article/details/39377903

//声明

 

private Iterator < String >  it = null; 

//使用时应该加一个判断,不然会报空指针异常

if (mySharedpreferences.getNum() != null) {
   it = mySharedpreferences.getNum().iterator();
  }

//在后面用时也加一个判断

if (it != null) {

//处理It

}

//唉,晚上弄到两点,就这一个空指针异常关于Iterator会报空指针的方法

展开阅读全文

listview下滑会报空指针

04-06

package jp.co.tre.pacer.shelfcheck;rnrnimport java.util.ArrayList;rnimport java.util.HashMap;rnimport java.util.List;rnrnimport jp.co.tre.pacer.shelfcheck.R;rnimport jp.co.trejapan.pacer.android.activity.PacerMainActivity;rnimport jp.co.trejapan.pacer.android.ui.FooterAdapter;rnimport android.content.Context;rnimport android.content.Intent;rnimport android.os.Bundle;rnimport android.view.LayoutInflater;rnimport android.view.View;rnimport android.view.ViewGroup;rnimport android.view.View.OnClickListener;rnimport android.widget.AdapterView;rnimport android.widget.AdapterView.OnItemClickListener;rnimport android.widget.ListView;rnimport android.widget.TextView;rnrnpublic class NewShelfReferActivity extends PacerMainActivity rn private List> list = new ArrayList>(); // 声明列表容器rn private HashMap item1 = new HashMap();rn private ListAdapter dataAdapter;rn private ListView listView;rn private TextView title;rn private int selectedPosition = 0;rnrn @Overridern public void onCreate(Bundle savedInstanceState) rnrn super.onCreate(savedInstanceState);rn setContentView(R.layout.shelf_refer);rn titleLayout.setTitleText(getString(R.string.title_name));rn pageinit();rn listInit();rn setFooterStyle();rn rnrn public void pageinit() rn listView = (ListView) findViewById(R.id.mylist_refer);rn title = (TextView) findViewById(R.id.title1);rn Intent intent = getIntent();rn String name = intent.getStringExtra("name");rn title.setText(name);rn for (int i = 0; i < 10; i++) rn HashMap skt = new HashMap();rn skt.put("name", "lll" + i);rn skt.put("id", "8101" + i);rn list.add(skt);rn rn rnrn public void listInit() rnrn dataAdapter = new ListAdapter(NewShelfReferActivity.this, list,rn R.layout.list_shelf_refer, new String[] "name", "id" ,rn new int[] R.id.shelfname, R.id.shelfno ) rn class ViewHolder rn TextView sname;rn TextView sno;rn rnrn public View getView(final int position, View convertView,rn ViewGroup parent) rn ViewHolder holder = null;rn View view = convertView;rn if (view == null) rn LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);rn view = inflater.inflate(R.layout.list_shelf_refer, null,rn false);rn holder = new ViewHolder();rn holder.sname = (TextView) view.findViewById(R.id.shelfname);rn holder.sno = (TextView) view.findViewById(R.id.shelfno);rnrn else rn holder = (ViewHolder) view.getTag();rn rn HashMap record = list.get(position);rn holder.sname.setText(record.get("name"));rn holder.sno.setText(record.get("id"));rn dataAdapter.notifyDataSetChanged();rn return view;rn rnrn ;rnrn listView.setAdapter(dataAdapter);rn listView.setOnItemClickListener(new OnItemClickListener() rnrn @Overridern public void onItemClick(AdapterView arg0, View arg1, int arg2,rn long arg3) rn selectedPosition = arg2;rn item1 = list.get(selectedPosition);rn Intent intent = new Intent();rn intent.setClass(NewShelfReferActivity.this,rn NewShelfLoginActivity.class);rn intent.putExtra("name", item1.get("name"));rn intent.putExtra("id", item1.get("id"));rn startActivity(intent);rnrn rn );rn rnrn // btn定义rn private void setFooterStyle() rn String[] btnText = new String[4];rn btnText[0] = getString(R.string.back);rn btnText[1] = "";rn btnText[2] = "";rn btnText[3] = "";rnrn OnClickListener[] listenner = new OnClickListener[4];rn listenner[0] = new OnClickListener() rnrn @Overridern public void onClick(View v) rn finish();rn System.exit(0);rn rn ;rnrn listenner[1] = new OnClickListener() rnrn @Overridern public void onClick(View v) rn rn ;rn listenner[2] = null;rn listenner[3] = new OnClickListener() rnrn @Overridern public void onClick(View arg0) rn // TODO Auto-generated method stubrnrn rnrn ;rn FooterAdapter adapter = new FooterAdapter(this, btnText, listenner);rn footer.setAdapter(adapter);[code=java][/code]rnrn rnrn 论坛

ServletActionContext.getRequest()为什么会报空指针

10-13

做一个项目的时候,用到了HttpSessionBindingListener监听session失效rn当运行valueBound的时候可以正常获取request,而当session失效运行valueUnbound的时候,获取request就会报空指针错误rn代码如下:rn[code=Java]public class SessionListener implements HttpSessionBindingListenerrn private User userObject;rn public SessionListener(User userObject) rn // TODO Auto-generated constructor stubrn this.userObject = userObject;rn rn //用户存sessionrn public void valueBound(HttpSessionBindingEvent event) rn // TODO Auto-generated method stubrn HttpSession session = event.getSession();rn session.setAttribute("userInfo", userObject);rn DateFormat df = DateFormat.getDateTimeInstance();rn System.out.println(df.format(new Date())+"绑定数据源:"+userObject.getUserName()+":"+session.getId()+":"+session.getCreationTime());rn try rn //这里就不会报错..奇怪rn System.out.println("测试一下获取request");rn HttpServletRequest request = ServletActionContext.getRequest();rn HttpSession session1 = request.getSession();rn catch (Exception e) rn // TODO: handle exceptionrn e.printStackTrace();rn rn rn rn // 用户SESSION失效,重新封装用户信息rn public void valueUnbound(HttpSessionBindingEvent event) rn //这里就会报错..rn HttpServletRequest request = ServletActionContext.getRequest();rn HttpSession session = request.getSession();rn rn rn public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue) rn for(int i=0; i 论坛

为什么log会报空指针异常错误?

05-16

我在编写添加背景音乐的程序时,参考了这里的代码http://apps.hi.baidu.com/share/detail/18496230rnrn我的工程中有两个类,一个是music类,继承activity,另一个是musicServer类,集成service。music类如下:rn[code=Java]package mx.music;rnrnimport android.app.Activity;rnimport android.content.Intent;rnimport android.os.Bundle;rnimport android.view.View;rnimport android.view.View.OnClickListener;rnimport android.widget.Button;rnrnpublic class music extends Activity rn /** Called when the activity is first created. */rn rn [color=#FF0000]private Intent intent = new Intent("mx.music.MUSIC");[/color]rn private Button startButton;rn private Button stopButton;rn @Overridern public void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.main);rn startButton = (Button) findViewById(R.id.startButton);rn stopButton = (Button) findViewById(R.id.stopButton);rn startButton.setOnClickListener(new startListener());rn stopButton.setOnClickListener(new stopListener());rn rn rn rn rn class startListener implements OnClickListenerrnrn public void onClick(View v) rn // TODO Auto-generated method stubrn [color=#FF0000] startService(intent);[/color]rn rn rn rn rn class stopListener implements OnClickListenerrnrn public void onClick(View v) rn // TODO Auto-generated method stubrn [color=#FF0000]stopService(intent);[/color]rn rn rn rn rn @Overridern protected void onPause() rn // TODO Auto-generated method stubrn super.onPause();rn rn rn rn rn rn rnrn[/code]rnrnmusicserver类如下:[code=Java]rnpackage mx.music;rnrnimport android.app.Service;rnimport android.content.Intent;rnimport android.media.MediaPlayer;rnimport android.os.IBinder;rnrnpublic class musicServer extends Servicernrn private MediaPlayer mp;rn @Overridern public IBinder onBind(Intent arg0) rn // TODO Auto-generated method stubrn return null;rn rn @Overridern public void onStart(Intent intent, int startId) rn // TODO Auto-generated method stubrn super.onStart(intent, startId);rn if(mp==null)rn mp=MediaPlayer.create(this, R.raw.mmp);rn [color=#FF0000]mp.start();[/color]/*显示这里空指针异常,但是我在上句都已经定义了啊,并且在raw中存在mmp的MP3文件*/rn rn rn @Overridern public void onDestroy() rn // TODO Auto-generated method stubrn super.onDestroy();rn mp.stop();rn rn rn rnrnrn[/code] 论坛

关于Iterator的next()方法

12-16

这种写法是不正确的,会报java.util.NoSuchElementException的错误。iterator.next()不能连续用,这个为什么rn[code=Java]Iterator iterator = list.iterator(); rn while(iterator.hasNext()) rn if(iterator.next().getCompanycode()==null)rn error.append("接口表中Companycode的必录项有空,");rn rn rn else if(iterator.next().getSalechannelcode()==null)rn error.append("接口表中Salechannelcode的必录项有空,");rn rn rn if(iterator.next().getBillnumber()==null)rn error.append("接口表中Billnumber的必录项有空,");rn rn rn if(iterator.next().getBillbooknumber()==null)rn error.append("接口表中Billbooknumber的必录项有空,");rn rn rn if(iterator.next().getSeragentcode()==null)rn error.append("接口表中Seragentcode的必录项有空,");rn rn rn if(iterator.next().getPhone()==null)rn error.append("接口表中Phone的必录项有空,");rn rn rn if(iterator.next().getSersectioncode()==null)rn error.append("接口表中Sersectioncode的必录项有空,");rn rn rn if(iterator.next().getSerteamcode()==null)rn error.append("接口表中Serteamcode的必录项有空,");rn rn rn if(iterator.next().getPolicyholdername()==null)rn error.append("接口表中Policyholdername的必录项有空,");rn rn rn if(iterator.next().getInsurecode()==null)rn error.append("接口表中Insurecode的必录项有空,");rn rn rn if(iterator.next().getCerttypecode()==null)rn error.append("接口表中Certtypecode的必录项有空,");rn rn rn if(iterator.next().getChargetypecode()==2&&iterator.next().getBankcode()==null)rn error.append("CHARGETYPECODE=2转帐时,BANKCODE不能为空,");rn rn rn if(iterator.next().getChargetypecode()==2&&iterator.next().getAccountname()==null)rn error.append("CHARGETYPECODE=2转帐时,Accountname不能为空,");rn rn rn if(iterator.next().getChargetypecode()==2&&iterator.next().getAccounts()==null)rn error.append("CHARGETYPECODE=2转帐时,Accounts不能为空,");rn rn rn if(iterator.next().getChargetype()==3&&iterator.next().getPeriodofvalidity()==null)rn error.append("ChargeType为信用卡时,PERIODOFVALIDITY, 信用卡缴费不能为空,");rn rn rn if(iterator.next().getChargetype()==3&&iterator.next().getPeriodofvalidity().matches("^\\d4$"))rn error.append("ChargeType为信用卡时,信用卡有效期至 格式不正确,");rn rn rn if (iterator.next().getChargetype()==3// 中信信用卡转帐时,Ensuremodel,计划型号必录rn &&iterator.next().getBrushbankcode().equals("017")rn &&iterator.next().getEnsuremodel()==null)rn error.append("中信信用卡转帐时,Ensuremodel,计划型号不能为空");rn rn rn [/code] 论坛

有关Iterator的问题,代码会报java.util.ConcurrentModificationException错

10-07

while(rs.next())rn rn rn Iterator iter = all.iterator();rn while (iter.hasNext()) rn Plan p = (Plan) iter.next();rn String id = p.getOtherFlag();rn if(rs.getString("otherflag").equals(id))rn rn System.out.println("存在相同");rn rn elsern rn //根据查找到符合条件的otherflag找到主表项rn String sql_res = "select * from administrator.tdphtmp where otherflag='"+ rs.getString("otherflag") +"'";rn System.out.println(sql_res);rn PreparedStatement pstmt_res = null ;rn tryrn rn pstmt_res = dbc.getConnection().prepareStatement(sql_res) ;rn ResultSet rs_res = pstmt_res.executeQuery() ;rn while(rs_res.next())rn rn Plan plan = new Plan() ;rn //plan.setId(rs.getInt("id"));rn plan.setDwbm_name(rs_res.getString("dwbm_name"));rn plan.setTxr(rs_res.getString("txr")) ;rn plan.setTxrq(rs_res.getDate("txrq")) ;rn plan.setDdselect(rs_res.getString("ddselect")) ;rn plan.setDd(rs_res.getString("dd"));rn plan.setTdsbfw(rs_res.getString("tdsbfw"));rn plan.setLb(rs_res.getString("lb"));rn plan.setGznr(rs_res.getString("gznr"));rn plan.setJhksrq(rs_res.getDate("jhksrq"));rn plan.setJhjsrq(rs_res.getDate("jhjsrq"));rn plan.setTs(rs_res.getString("ts"));rn plan.setWorktimesure(rs_res.getString("worktimesure"));rn plan.setBz(rs_res.getString("bz"));rn plan.setSjzt(rs_res.getString("sjzt"));rn plan.setScbj(rs_res.getString("scbj"));rn plan.setOtherFlag(rs_res.getString("otherflag"));rn plan.setOtherFlag2(rs_res.getString("otherflag2"));rn all.add(plan) ;rn rn rs_res.close() ;rn pstmt_res.close() ;rn rn catch (Exception e)rn rn System.out.println("query出现错误"+e) ;rn rn rn rn rn rn rs.close() ;rn pstmt.close() ;rn 论坛

各位大手帮忙看下为啥return mFragments.length;会报空指针

10-28

[code=java]package com.new_apps;rnrnimport android.graphics.Color;rnimport android.os.Bundle;rnimport android.support.v4.app.Fragment;rnimport android.support.v4.app.FragmentActivity;rnimport android.support.v4.app.FragmentManager;rnimport android.support.v4.app.FragmentPagerAdapter;rnimport android.support.v4.view.ViewPager.OnPageChangeListener;rnimport android.view.View;rnimport android.view.View.OnClickListener;rnimport android.widget.Button;rnimport android.widget.TextView;rnrnimport com.animation.settings.VerticalViewPager;rnimport com.example.new_apps.R;rnimport com.fragment.page.Fragment_firstpage;rnimport com.fragment.page.Fragment_secondpage;rnimport com.fragment.page.Fragment_thirdpage;rnrnpublic class MainActivity extends FragmentActivity implements OnClickListener rn rn // 定义Fragmentrn Fragment f1 = Fragment_firstpage.newInstance();rn Fragment f2 = Fragment_secondpage.newInstance();rn Fragment f3 = Fragment_thirdpage.newInstance();rnrn private VerticalViewPager viewpager;rn private Button tv1, tv2, tv3;rnrn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.activity_main);rn viewpager = (VerticalViewPager) findViewById(R.id.viewpager);rn tv1 = (Button) findViewById(R.id.tv1);rn tv2 = (Button) findViewById(R.id.tv2);rn tv3 = (Button) findViewById(R.id.tv3);rn tv1.setOnClickListener(this);rn tv2.setOnClickListener(this);rn tv3.setOnClickListener(this);rn viewpager.setAdapter(new TempAdapter(getSupportFragmentManager()));// 设置viewpager适配器rn viewpager.setCurrentItem(0);//默认为第一页rn viewpager.setOnPageChangeListener(mOnPageChangeListener);// 设置viewpager切换监听rn rn rn //viewpager切换监听rn OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() rn @Overridern public void onPageSelected(int position) rn switchViewPager(position);//设置切换rn rn @Overridern public void onPageScrolled(int arg0, float arg1, int arg2) rn rn rn @Overridern public void onPageScrollStateChanged(int arg0) rn rn ;rnrnrn // ViewPager适配器rn private final class TempAdapter extends FragmentPagerAdapter rn private Fragment[] mFragments;rnrn public TempAdapter(FragmentManager fm) rn super(fm);rn mFragments = new Fragment[] f1,f2,f3;// Fragment加入集合rn rnrn @Overridern public Fragment getItem(int position) rn return mFragments[position];rn rnrn @Overridern public int getCount() rn // TODO Auto-generated method stubrn return mFragments.length;//rn rn rnrn rnrn @Overridern public void onClick(View v) rn switch (v.getId()) rn case R.id.tv1:rn switchViewPager(0);rn break;rn case R.id.tv2:rn switchViewPager(1);rn break;rn case R.id.tv3:rn switchViewPager(2);rn break;rn case R.id.tv4:rn switchViewPager(3);rn break;rn default:rn break;rn rnrn rnrn // 自定义切换ViewPagerrn private void switchViewPager(int position) rn viewpager.setCurrentItem(position);rn switch (position) rn case 0:rn// tv1.setBackgroundColor(Color.parseColor("#FF3030"));rn// tv2.setBackgroundColor(Color.parseColor("#48D1CC"));rn break;rn case 1:rn// tv1.setBackgroundColor(Color.parseColor("#48D1CC"));rn// tv2.setBackgroundColor(Color.parseColor("#FF3030"));rn break;rn case 2:rn// tv1.setBackgroundColor(Color.parseColor("#48D1CC"));rn// tv2.setBackgroundColor(Color.parseColor("#FF3030"));rn break;rn case 3:rn// tv1.setBackgroundColor(Color.parseColor("#48D1CC"));rn// tv2.setBackgroundColor(Color.parseColor("#FF3030"));rn break;rn case 4:rn// tv1.setBackgroundColor(Color.parseColor("#48D1CC"));rn// tv2.setBackgroundColor(Color.parseColor("#FF3030"));rn break;rn default:rn break;rn rn rnrn[/code] 论坛

一个weblogic的问题,网站正常运行一段时间后,会报空指针

07-02

我做了一个jsp的网站,挂在weblogic下,连oracle数据库rnrn但是经常在正常使用了一段时间后,就会发生错误,几乎每次访问任何动态页面,都会报空指针,如下所示,这一般会是什么问题rnrn而且,出现错误后,重启weblogic,又恢复正常了。请大家给点意见.rnrn<2007-7-2 下午03时06分49秒 CST> <[ServletContext(id=rn9090849,name=webpdda,context-path=/webpdda)] Servlet failed with Exceptionrnjava.lang.NullPointerExceptionrn at jsp_servlet.__fwzn_in._jspService(__fwzn_in.java:165)rn at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)rn at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.runrn(ServletStubImpl.java:996)rn at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImrnpl.java:419)rn at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImrnpl.java:315)rn at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActiornn.run(WebAppServletContext.java:6452)rn at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticaterndSubject.java:321)rn at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:rn118)rn at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSernrvletContext.java:3661)rn at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImrnpl.java:2630)rn at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)rn at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)rn>rn 论坛

关于空指针的问题。

05-30

public ActionForward add(ActionMapping mapping, ActionForm form,rn HttpServletRequest request, HttpServletResponse response) rn GoodClassAddForm goodClassAddForm = (GoodClassAddForm) form;rn String firstName = goodClassAddForm.getFirstName();rn String firstClass = goodClassAddForm.getFirstClass();rn Gclasses gclasses = new Gclasses();rn gclasses.setGclaName(firstName);rn gclasses.setGclaGrade(firstClass);rn gclasses.setGclaLevel("1");rn boolean check = gclassesinterface.save(gclasses);rn if (check == true) rn String information = "添加大类成功!";rn request.setAttribute("addClassInformation", information);rn else rn String information = "添加大类失败,请重新添加!";rn request.setAttribute("addClassInformation", information);rn rn return mapping.findForward("addClassSuccess");rn rn这样做的话直接运行.do文件会在数据库中插入空字符串rnpublic ActionForward add(ActionMapping mapping, ActionForm form,rn HttpServletRequest request, HttpServletResponse response) rn GoodClassAddForm goodClassAddForm = (GoodClassAddForm) form;rn rn if(goodClassAddForm.getFirstName()==""||goodClassAddForm.getFirstClass().equals(null)rn )rn rn String information="名称或者类编号不能为空!请重新添加";rn request.setAttribute("addClassInformation", information);rn return mapping.findForward("addClassSuccess");rn rn String firstName = goodClassAddForm.getFirstName();rn String firstClass = goodClassAddForm.getFirstClass();rn Gclasses gclasses = new Gclasses();rn gclasses.setGclaName(firstName);rn gclasses.setGclaGrade(firstClass);rn gclasses.setGclaLevel("1");rn boolean check = gclassesinterface.save(gclasses);rn if (check == true) rn String information = "添加大类成功!";rn request.setAttribute("addClassInformation", information);rn else rn String information = "添加大类失败,请重新添加!";rn request.setAttribute("addClassInformation", information);rn rn return mapping.findForward("addClassSuccess");rn rn这样做的话直接运行.do文件出现的是空指针错误,请问这是为什么啊? ssh结构 论坛

没有更多推荐了,返回首页