空指针错误解决不了了 ssi框架 就想写在页面里输入内容 提交到表里 结果表单一提交 就这样了 沾了部分代码 求高手帮忙 菜鸟小弟多谢了
错误如下
严重: Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at com.javasky.action.addWeapons.add(addWeapons.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
。。。。。。。。。。
我的配置如下
applicationContext如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:MYORACLE</value>
</property>
<property name="username">
<value>MYD</value>
</property>
<property name="password">
<value>12345</value>
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:SqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="weapons" class="com.javasky.bean.weapons"></bean>
<bean id="wdaoimpl"
class="com.javasky.dao.impl.weaponsDaoImpl">
<property name="sqlmapclient" ref="sqlMapClient"/>
<property name="gun" ref="weapons"/>
</bean>
<bean id="wserviceimpl"
class="com.javasky.service.impl.weaponserviceimpl">
<property name="weapons" ref="wdaoimpl"/>
<property name="gun" ref="weapons"/>
</bean>
<bean id="waddweapons" class="com.javasky.action.addWeapons">
<property name="ws" ref="wserviceimpl"></property>
<property name="gun" ref="weapons"/>
</bean>
<bean id="wqueryWeapons" class="com.javasky.action.queryWeapons">
<property name="ws" ref="wserviceimpl"></property>
</bean>
</beans>
struts.xml代码如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts
Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="package1" extends="struts-default">
<action name="test" class="com.javasky.action.addWeapons">
<result name="add">/jsp/showWeapons.jsp</result>
</action>
<action name="test1" class="com.javasky.action.queryWeapons">
<result name="list">/jsp/showWeapons.jsp</result>
</action>
</package>
</struts>
web.xml代码如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我的实体bean如下
package com.javasky.bean;
public class weapons {
private String id;
private String name;
private String prize;
private String weight;
public String getId() {
return id;
}
public void setId(String id2) {
this.id = id2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
我的DaoImpl
package com.javasky.dao.impl;
import java.sql.SQLException;
import java.util.List;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.javasky.bean.weapons;
import com.javasky.dao.weaponsDao;
public class weaponsDaoImpl implements weaponsDao{
private SqlMapClient sqlmapclient;
private weapons gun;
public void insertWeapon(weapons gun)
{
try {
sqlmapclient.insert("test.insertweapon", gun);
} catch (SQLException e) {
e.printStackTrace();
}
}
public SqlMapClient getSqlmapclient() {
return sqlmapclient;
}
public void setSqlmapclient(SqlMapClient sqlmapclient) {
this.sqlmapclient = sqlmapclient;
}
public weapons getGun() {
return gun;
}
public void setGun(weapons gun) {
this.gun = gun;
};
public List queryAll(){
List list = null;
try {
list =sqlmapclient.queryForList("test.queryWeapon", gun);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
};
}
我的serviceImpl
package com.javasky.service.impl;
import java.util.List;
import com.javasky.service.weaponservice;
import com.javasky.bean.*;
import com.javasky.dao.weaponsDao;
public class weaponserviceimpl implements weaponservice{
private weaponsDao weapons;
private weapons gun;
public weaponsDao getWeapons() {
return weapons;
}
public void setWeapons(weaponsDao weapons) {
this.weapons = weapons;
}
public void insertWeapon(weapons gun)
{
weapons.insertWeapon(gun);
}
public List queryAll()
{
List list=weapons.queryAll();
return list;
}
public void setGun(weapons gun) {
this.gun = gun;
}
public weapons getGun() {
return gun;
}
}
我的action层
package com.javasky.action;
import com.javasky.service.weaponservice;
import com.javasky.bean.*;
import com.opensymphony.xwork2.ActionSupport;
public class addWeapons extends ActionSupport {
private weaponservice ws;
private weapons gun;
private String name;
private String id;
private String prize;
private String weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public weaponservice getWs() {
return ws;
}
public void setWs(weaponservice ws) {
this.ws = ws;
}
public weapons getGun() {
return gun;
}
public void setGun(weapons gun) {
this.gun = gun;
}
public String add()
{
System.out.println(gun.getId());
System.out.println(gun.getName());
System.out.println(gun.getPrize());
System.out.println(gun.getWeight());
/* gun.setId(id);
gun.setName(name);
gun.setPrize(prize);
gun.setWeight(weight);*/
ws.insertWeapon(gun);
return "add";
}
}
我的weapons.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="test">
<select id="insertweapon" >
<![CDATA[
insert into weapons (id,name,prize,weight)values(#id#,#name#,#prize#,#weight#)
]]>
</select>
<select id="queryWeapon" resultClass="list">
<![CDATA[
select * from weapons
]]>
</select>
</sqlMap>
dao service接口代码没粘
jsp中
addWeapons.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="<%=request.getContextPath()%>/test!add.action" method="post">
<input type="text" name="gun.id">输入枪的编号</input><br/>
<input type="text" name="gun.name" >输入枪的名称</input></br>
<input type="text" name="gun.prize" >输入枪的价钱</input></br>
<input type="text" name="gun.weight" >输入枪的重量</input>
<input type="submit" value="添加到数据库"/>
</form>
</body>
</html>
[解决办法]
你看看出错行代码写的什么,然后先打印一下。
com.javasky.action.addWeapons.add(addWeapons.java:87)
你就看addWeapons类的add方法,在类的87行是什么代码?
是不是System.out.println(gun.getId());
这个代码?
如果是这个你先打印一下gun看看是不是没有创建对象,是不是为null。
[解决办法]
这么多代码,这不是大海捞针吗,你打个断点一步步向下跟,定位到抛异常的位置,然后把附近的代码贴出来,这样看不是很容易吗。
[解决办法]
这都什么乱七八糟的一堆,如果ws.insertWeapon(gun)报错很简单,ws为空
注入错误,首先变量名不要太短,其次类名还小写?