0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【初心者向け】エラーログの読み方入門 ─ エンジニアに役立つ基本の4ステップ

Posted at

プログラミングをしていると、必ずと言っていいほど出会う「エラーログ」。
でも初心者の頃は、「英語ばかりで難しい…」「何から読めばいいの?」と感じがちです。

この記事では、どんな言語やフレームワークにも共通するエラーログの読み方の基本を、実例付きでわかりやすく解説します!


✅ まずはこの4ステップを押さえよう!

ステップ 見るポイント
① エラーの種類 どんなエラー?(例:NullPointerException、SyntaxErrorなど)
② エラーメッセージ 何が原因?何をしようとして失敗した?
③ スタックトレース どの順番で、どこで失敗した?
④ ファイル・行番号 自分のコードのどこを直せばいい?

今回はこのコードを例に解説していきます。

TypeError: Cannot read properties of undefined (reading 'foo')
    at Object.<anonymous> (/Users/user/project/index.js:10:15)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    ...

① エラーの種類を見る

TypeError: Cannot read properties of undefined (reading 'foo')
  • TypeError は「型」に関するエラー。
  • undefined な変数に対して .foo とプロパティを読もうとして失敗しています。

🟢 この時点で「たぶん初期化されてない変数を使ったかも」と予想が立ちます!


② エラーメッセージを読む

Cannot read properties of undefined (reading 'foo')
  • undefined の中に foo というプロパティはないよ!という意味。
  • つまり、foo を読み取ろうとして失敗。

③ スタックトレースを見る(下から上に)

    at Object.<anonymous> (/Users/user/project/index.js:10:15)
    at Module._compile (...)
  • 最初に注目するのは index.js:10:15
  • ここが実際にエラーが起きた場所

自分が書いたコードが出てくる行をまずチェック!


④ ファイル名と行番号で場所を特定

at /Users/user/project/index.js:10:15
  • index.js の10行目15文字目でエラーが発生。

デバッグするときは、その近くで console.log() を使って値を確認するとGood!


より具体的なエラー全文(実例)

JavaScriptのエラー全文(TypeError)

TypeError: Cannot read properties of undefined (reading 'foo')
    at Object.<anonymous> (/Users/user/project/index.js:10:15)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

解説:このエラーの読み方と原因特定

  1. エラーの種類: TypeError

    • JavaScriptで"想定しない型の操作"を行おうとしたときのエラーです。
  2. メッセージ: Cannot read properties of undefined (reading 'foo')

    • undefined であるオブジェクトから foo プロパティを読もうとした。
    • よくある例:オブジェクトが nullundefined のときに .foo を参照。
  3. 原因の場所:

    at Object.<anonymous> (/Users/user/project/index.js:10:15)
    
    • index.js10行目の15文字目 で発生している。
  4. 対処法:

    • 該当箇所を console.log() でデバッグし、undefined になっている原因を調査。

    • 例えば user という変数が undefined なら、

      console.log(user); // undefined
      console.log(user?.foo); // Optional chainingで回避も可能
      
    • APIの戻り値や、props、初期化漏れを確認しましょう。

Javaのエラー全文(NullPointerException)

Exception in thread "main" java.lang.NullPointerException
    at com.example.MyClass.doSomething(MyClass.java:15)
    at com.example.Main.main(Main.java:8)

解説:このエラーの読み方と原因特定

  1. エラーの種類: NullPointerException

    • Javaで null の変数やオブジェクトにアクセスしようとしたときに発生します。
  2. スタックトレース:

    at com.example.MyClass.doSomething(MyClass.java:15)
    
    • MyClass.java の15行目で null なオブジェクトに対して .メソッド() を呼び出してしまっている。
  3. 対処法:

    • null チェックを追加する、もしくはインスタンス化を確認する。
    • 例:if (obj != null) { obj.doSomething(); }

✅ 初期化漏れや、戻り値が null になっているケースが非常に多いので、呼び出し前の状態を確認しましょう。

PHPのエラー全文(Fatal Error)

Fatal error: Uncaught Error: Call to undefined function my_undefined_function() in /var/www/html/index.php:5
Stack trace:
#0 {main}
  thrown in /var/www/html/index.php on line 5

解説:このエラーの読み方と原因特定

  1. エラーの種類: Fatal error

    • PHPがスクリプトの実行を止めるほど重大なエラーです。
  2. エラー内容: Call to undefined function my_undefined_function()

    • 存在しない関数を呼び出してしまった。
  3. 発生箇所: /var/www/html/index.php:5

    • 5行目でその関数が呼ばれている。
  4. 対処法:

    • 関数名のスペルミスを確認
    • 関数が定義されたファイルが読み込まれているか(require, include, autoload)を確認

✅ 特に関数定義ファイルの読み込み忘れが原因になっていることが多いです。

エラーを解決するための具体的なアクション集

  • エラーメッセージの キーワードや単語 を使ってGoogle検索する

  • ChatGPTなどのAIにログ全文を貼って相談する

    • 例:「このエラーログの意味と対処法を教えてください」
    • 環境(OS・言語・フレームワーク)を伝えるとより的確な回答が得られやすい
    • 例:「このエラーログの原因と直し方を教えてください」
    • 実行環境や使用中のフレームワークも一緒に伝えるとより正確

まとめ

以下の順に読むのがポイント:

  1. エラーの種類 → どんな問題?
  2. メッセージ → 何が起きた?
  3. スタックトレース → どこで失敗?
  4. ファイル名と行番号 → 修正すべき場所!

この記事が良かったら、ストックやいいねをしてもらえると嬉しいです!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?