Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我在这段代码中得到nullpointerexception? 在此处输入代码 导入java.io.*; 导入java.util.*; 导入java.text.*; 导入java.math.*; 导入java.util.regex.*; 公共类解决方案 { 静态布尔值[]; 静态int顶点; 静态布尔树[]; 公共静态void main(字符串[]args) { /*在此处输入代码。从STDIN读取输入。将输出打印到STDOUT。您的类应命名为Solution*/ 扫描仪sc=新的扫描仪(System.in); 顶点=sc.nextInt(); int edges=sc.nextInt(); 已访问=新布尔值[顶点+1]; 布尔树[][]=新布尔[顶点+1][顶点+1]; 对于(int k=0;k_Java_Breadth First Search - Fatal编程技术网

为什么我在这段代码中得到nullpointerexception? 在此处输入代码 导入java.io.*; 导入java.util.*; 导入java.text.*; 导入java.math.*; 导入java.util.regex.*; 公共类解决方案 { 静态布尔值[]; 静态int顶点; 静态布尔树[]; 公共静态void main(字符串[]args) { /*在此处输入代码。从STDIN读取输入。将输出打印到STDOUT。您的类应命名为Solution*/ 扫描仪sc=新的扫描仪(System.in); 顶点=sc.nextInt(); int edges=sc.nextInt(); 已访问=新布尔值[顶点+1]; 布尔树[][]=新布尔[顶点+1][顶点+1]; 对于(int k=0;k

为什么我在这段代码中得到nullpointerexception? 在此处输入代码 导入java.io.*; 导入java.util.*; 导入java.text.*; 导入java.math.*; 导入java.util.regex.*; 公共类解决方案 { 静态布尔值[]; 静态int顶点; 静态布尔树[]; 公共静态void main(字符串[]args) { /*在此处输入代码。从STDIN读取输入。将输出打印到STDOUT。您的类应命名为Solution*/ 扫描仪sc=新的扫描仪(System.in); 顶点=sc.nextInt(); int edges=sc.nextInt(); 已访问=新布尔值[顶点+1]; 布尔树[][]=新布尔[顶点+1][顶点+1]; 对于(int k=0;k,java,breadth-first-search,Java,Breadth First Search,这是您的问题: enter code here import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static boolean visited[]; static int vertices; static boolean tree[][]; pub

这是您的问题:

    enter code here
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution 
{
    static boolean visited[];
    static int vertices;
    static boolean tree[][];

    public static void main(String[] args)
     {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);

        vertices=sc.nextInt();
        int edges=sc.nextInt();

        visited=new boolean[vertices+1];


        boolean tree[][]=new boolean[vertices+1][vertices+1];
        for (int k=0;k<vertices+1 ;k++ ) 
        {
            for (int j=0;j<vertices+1 ;j++ )
            {
                tree[k][j]=false;
            } 

        }

        int count[]=new int[vertices+1];
        //Arrays.fill(count,1);

        for(int i=0;i<edges;i++)
            {
            int u=sc.nextInt();
            int v=sc.nextInt();

            tree[v][u]=true;



        }

        for (int i=0;i<vertices+1 ;i++ ) 
        {
            count[i]=bfs(i) + 1;             //getting error at this line

        }
        int finalcount=0;
        for (int i=0;i<vertices+1 ;i++ ) 
        {
            if (count[i]%2==0) 
            {
                finalcount++;

            }

        }

        System.out.println(finalcount);


    }

  public static int bfs(int node)
      {
      Queue<Integer> q=new LinkedList<Integer>();
      for(int i=0;i<vertices+1;i++)
          {
          visited[i]=false;
      }
      visited[0]=true;
      int counter=0;
      q.add(node);
      while( !q.isEmpty() )
      {
         int nextNode;                // Next node to visit
         int i;

         nextNode = Integer.valueOf(q.remove());

         if ( ! visited[nextNode] )
         {
            visited[nextNode] = true;    //mark visited


            for ( i = 0; i < vertices+1; i++ )
             {  
                if ( tree[nextNode][i] && ! visited[i] ) //getting error at this line too
                {  
                    q.add(i);
                    counter++;
                }
             }
         }
      }

      return counter;
  }
}





/*

10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8
ans:2


20 19
2 1
3 1
4 3
5 2
6 5
7 1
8 1
9 2
10 7
11 10
12 3
13 7
14 8
15 12
16 6
17 6
18 10
19 1
20 8
ans:4



*/
您可以初始化本地
数组,而不是静态
成员,该成员保持为空

将其更改为:

public class Solution 
{
    ...
    static boolean tree[][];
    ...
    public static void main(String[] args)
    {
        ...
        boolean tree[][]=new boolean[vertices+1][vertices+1];
        ...

在这里发布堆栈跟踪。哪一行导致了问题?谢谢。刚刚意识到。
public class Solution 
{
    ...
    static boolean tree[][];
    ...
    public static void main(String[] args)
    {
        ...
        tree = new boolean[vertices+1][vertices+1];
        ...