위 와 같은 java.lang.NullPointerException: Cannot invoke "pack.product.ProductDto.getName()" because "product" is null
에러코드가 발생하였다.
코드의 문제인줄 알고 이전에 작성한 코드들을 찾아봤지만 코드의 문제가 아니었고 DB의 문제였다.
MariaDB의 Command Prompt를 열어
MariaDB [shopdb]> select * from shop_product;
+----+----------------------+--------+------------------------------------+---------------------+-------+-------------+
| no | name | price | detail | sdate | stock | image |
+----+----------------------+--------+------------------------------------+---------------------+-------+-------------+
| 4 | 튼튼책상 | 450000 | 튼튼한 4다리 책상입니다 | 2023-08-28 16:20:49 | 100 | desk.jpg |
| 5 | 화난 풍선 복어 | 100000 | 풍선이 된 풍선 가시복어 | 2023-08-28 16:21:20 | 992 | fish.gif |
| 6 | 군대 슬리퍼 | 20000 | 비브라늄 재질의 슬리퍼 | 2023-08-28 16:21:51 | 998 | slipper.gif |
+----+----------------------+--------+------------------------------------+---------------------+-------+-------------+
3 rows in set (0.000 sec)
MariaDB [shopdb]> select * from shop_product where no=4;
+----+--------------+--------+-----------------------------------+---------------------+-------+----------+
| no | name | price | detail | sdate | stock | image |
+----+--------------+--------+-----------------------------------+---------------------+-------+----------+
| 4 | 튼튼책상 | 450000 | 튼튼한 4다리 책상입니다 | 2023-08-28 16:20:49 | 100 | desk.jpg |
+----+--------------+--------+-----------------------------------+---------------------+-------+----------+
1 row in set (0.000 sec)
MariaDB [shopdb]> delete from shop_product;
Query OK, 3 rows affected (0.005 sec)
MariaDB [shopdb]> select * from shop_product;
Empty set (0.000 sec)
MariaDB [shopdb]> select * from shop_product;
+----+------------------+--------+--------+---------------------+-------+-----------+
| no | name | price | detail | sdate | stock | image |
+----+------------------+--------+--------+---------------------+-------+-----------+
| 12 | 가정용 소파 | 500000 | dfdf | 2023-08-30 11:17:01 | 99 | desk2.jpg |
+----+------------------+--------+--------+---------------------+-------+-----------+
1 row in set (0.000 sec)
MariaDB [shopdb]> drop table shop_productu;
ERROR 1051 (42S02): Unknown table 'shopdb.shop_productu'
MariaDB [shopdb]> drop table shop_product;
Query OK, 0 rows affected (0.013 sec)
MariaDB [shopdb]> drop table shop_order;
Query OK, 0 rows affected (0.009 sec)
MariaDB [shopdb]> show tables;
+------------------+
| Tables_in_shopdb |
+------------------+
| admin |
| board |
| member |
| ziptab |
+------------------+
4 rows in set (0.004 sec)
MariaDB [shopdb]> CREATE TABLE shop_product (
-> no int(11) NOT NULL AUTO_INCREMENT,
-> name varchar(20),
-> price varchar(10),
-> detail text,
-> sdate datetime,
-> stock varchar(10),
-> image varchar(20),
-> PRIMARY KEY (no)
-> );
Query OK, 0 rows affected (0.015 sec)
MariaDB [shopdb]> CREATE TABLE shop_order (
->
-> no int auto_increment,
->
-> product_no varchar(5) NOT NULL,
->
-> quantity varchar(10) NULL,
->
-> sdate datetime NULL,
->
-> state varchar(10) NULL,
->
-> id varchar(10) NULL,
->
-> PRIMARY KEY (no));
Query OK, 0 rows affected (0.010 sec)
코드에서의 문제가 아닌 DB에서 문제이다.
맘편하게 사용중인 table인 shop_product
, shop_order
을 drop문을 통해 삭제하고
다시 create
문을 사용하여 테이블을 생성하였더니 에러가 사라졌다.
- 상품에 대해 오더로 주문을 했다.
- 오더로 주문하면서 오더로는 들어갔지만
DB안에 있는 코드에서 에러가 발생하였다.
때문에 DB속 오더로 들어갔지만 쓸모없어지게 되었다.
쓸데없는 레코드(코드 에러)가 껴저서 그 DB가 에러가 난것이다.(order와 product가 연결되어 있는데 레코드가 잘못 껴저 있어서 오류발생)
고생많으셨습니다!